2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 #include <IOKit/IOWorkLoop.h>
26 #include <IOKit/IOCommandGate.h>
27 #include <IOKit/IOTimerEventSource.h>
28 #include <IOKit/IOPlatformExpert.h>
29 #include <IOKit/IOKitDebug.h>
30 #include <IOKit/IOTimeStamp.h>
31 #include <IOKit/pwr_mgt/RootDomain.h>
32 #include <IOKit/pwr_mgt/IOPMPrivate.h>
33 #include <IOKit/IOMessage.h>
34 #include "RootDomainUserClient.h"
35 #include "IOKit/pwr_mgt/IOPowerConnection.h"
37 extern "C" void kprintf(const char *, ...);
39 extern const IORegistryPlane
* gIOPowerPlane
;
41 // debug trace function
43 ioSPMTrace(unsigned int csc
,
44 unsigned int a
= 0, unsigned int b
= 0,
45 unsigned int c
= 0, unsigned int d
= 0)
47 if (gIOKitDebug
& kIOLogTracePower
)
48 IOTimeStampConstant(IODBG_POWER(csc
), a
, b
, c
, d
);
51 IOReturn
broadcast_aggressiveness ( OSObject
*, void *, void *, void *, void * );
52 static void sleepTimerExpired(thread_call_param_t
);
53 static void wakeupClamshellTimerExpired ( thread_call_param_t us
);
56 #define number_of_power_states 5
58 #define RESTART_STATE 1
63 #define ON_POWER kIOPMPowerOn
64 #define RESTART_POWER kIOPMRestart
65 #define SLEEP_POWER kIOPMAuxPowerOn
66 #define DOZE_POWER kIOPMDoze
68 static IOPMPowerState ourPowerStates
[number_of_power_states
] = {
69 {1,0, 0, 0,0,0,0,0,0,0,0,0}, // state 0, off
70 {1,kIOPMRestartCapability
, kIOPMRestart
, RESTART_POWER
,0,0,0,0,0,0,0,0}, // state 1, restart
71 {1,kIOPMSleepCapability
, kIOPMSleep
, SLEEP_POWER
,0,0,0,0,0,0,0,0}, // state 2, sleep
72 {1,kIOPMDoze
, kIOPMDoze
, DOZE_POWER
,0,0,0,0,0,0,0,0}, // state 3, doze
73 {1,kIOPMPowerOn
, kIOPMPowerOn
, ON_POWER
,0,0,0,0,0,0,0,0}, // state 4, on
76 static IOPMrootDomain
* gRootDomain
;
77 static UInt32 gSleepOrShutdownPending
= 0;
80 #define super IOService
81 OSDefineMetaClassAndStructors(IOPMrootDomain
,IOService
)
85 IONotifier
* registerSleepWakeInterest(IOServiceInterestHandler handler
, void * self
, void * ref
= 0)
87 return gRootDomain
->registerInterest( gIOGeneralInterest
, handler
, self
, ref
);
90 IONotifier
* registerPrioritySleepWakeInterest(IOServiceInterestHandler handler
, void * self
, void * ref
= 0)
92 return gRootDomain
->registerInterest( gIOPriorityPowerStateInterest
, handler
, self
, ref
);
95 IOReturn
acknowledgeSleepWakeNotification(void * PMrefcon
)
97 return gRootDomain
->allowPowerChange ( (unsigned long)PMrefcon
);
100 IOReturn
vetoSleepWakeNotification(void * PMrefcon
)
102 return gRootDomain
->cancelPowerChange ( (unsigned long)PMrefcon
);
105 IOReturn
rootDomainRestart ( void )
107 return gRootDomain
->restartSystem();
110 IOReturn
rootDomainShutdown ( void )
112 return gRootDomain
->shutdownSystem();
115 void IOSystemShutdownNotification ( void )
117 for ( int i
= 0; i
< 100; i
++ )
119 if ( OSCompareAndSwap( 0, 1, &gSleepOrShutdownPending
) ) break;
124 int sync_internal(void);
128 A device is always in the highest power state which satisfies its driver, its policy-maker, and any power domain
129 children it has, but within the constraint of the power state provided by its parent. The driver expresses its desire by
130 calling changePowerStateTo(), the policy-maker expresses its desire by calling changePowerStateToPriv(), and the children
131 express their desires by calling requestPowerDomainState().
133 The Root Power Domain owns the policy for idle and demand sleep and doze for the system. It is a power-managed IOService just
134 like the others in the system. It implements several power states which correspond to what we see as Sleep, Doze, etc.
136 The sleep/doze policy is as follows:
137 Sleep and Doze are prevented if the case is open so that nobody will think the machine is off and plug/unplug cards.
138 Sleep and Doze are prevented if the sleep timeout slider in the preferences panel is at zero.
139 The system cannot Sleep, but can Doze if some object in the tree is in a power state marked kIOPMPreventSystemSleep.
141 These three conditions are enforced using the "driver clamp" by calling changePowerStateTo(). For example, if the case is
142 opened, changePowerStateTo(ON_STATE) is called to hold the system on regardless of the desires of the children of the root or
143 the state of the other clamp.
145 Demand Sleep/Doze is initiated by pressing the front panel power button, closing the clamshell, or selecting the menu item.
146 In this case the root's parent actually initiates the power state change so that the root has no choice and does not give
147 applications the opportunity to veto the change.
149 Idle Sleep/Doze occurs if no objects in the tree are in a state marked kIOPMPreventIdleSleep. When this is true, the root's
150 children are not holding the root on, so it sets the "policy-maker clamp" by calling changePowerStateToPriv(ON_STATE)
151 to hold itself on until the sleep timer expires. This timer is set for the difference between the sleep timeout slider and
152 the larger of the display dim timeout slider and the disk spindown timeout slider in the Preferences panel. For example, if
153 the system is set to sleep after thirty idle minutes, and the display and disk are set to sleep after five idle minutes,
154 when there is no longer an object in the tree holding the system out of Idle Sleep (via kIOPMPreventIdleSleep), the root
155 sets its timer for 25 minutes (30 - 5). When the timer expires, it releases its clamp and now nothing is holding it awake,
158 Demand sleep is prevented when the system is booting. When preferences are transmitted by the loginwindow at the end of
159 boot, a flag is cleared, and this allows subsequent Demand Sleep.
161 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
162 a clamp, but sets a flag which is noticed before actually sleeping the kernel. If the flag is set, the root steps up
163 one power state from Sleep to Doze, and any objects in the tree for which this is relevent will act appropriately (USB and
164 ADB will turn on again so that they can wake the system out of Doze (keyboard/mouse activity will cause the Display Wrangler
169 // **********************************************************************************
171 IOPMrootDomain
* IOPMrootDomain::construct( void )
173 IOPMrootDomain
* root
;
175 root
= new IOPMrootDomain
;
182 // **********************************************************************************
184 static void disk_sync_callout(thread_call_param_t p0
, thread_call_param_t p1
)
186 IOService
* rootDomain
= (IOService
*) p0
;
187 unsigned long pmRef
= (unsigned long) p1
;
190 rootDomain
->allowPowerChange(pmRef
);
193 // **********************************************************************************
196 // We don't do much here. The real initialization occurs when the platform
197 // expert informs us we are the root.
198 // **********************************************************************************
201 bool IOPMrootDomain::start ( IOService
* nub
)
203 OSDictionary
*tmpDict
;
210 setProperty("IOSleepSupported","");
212 sleepIsSupported
= true;
213 systemBooting
= true;
214 ignoringClamshell
= true;
216 idleSleepPending
= false;
220 ignoringClamshellDuringWakeup
= false;
222 tmpDict
= OSDictionary::withCapacity(1);
223 setProperty(kRootDomainSupportedFeatures
, tmpDict
);
226 pm_vars
->PMworkloop
= IOWorkLoop::workLoop(); // make the workloop
227 extraSleepTimer
= thread_call_allocate((thread_call_func_t
)sleepTimerExpired
, (thread_call_param_t
) this);
228 clamshellWakeupIgnore
= thread_call_allocate((thread_call_func_t
)wakeupClamshellTimerExpired
, (thread_call_param_t
) this);
229 diskSyncCalloutEntry
= thread_call_allocate(&disk_sync_callout
, (thread_call_param_t
) this);
231 patriarch
= new IORootParent
; // create our parent
233 patriarch
->attach(this);
234 patriarch
->start(this);
235 patriarch
->youAreRoot();
236 patriarch
->wakeSystem();
237 patriarch
->addPowerChild(this);
239 registerPowerDriver(this,ourPowerStates
,number_of_power_states
);
241 setPMRootDomain(this);
242 changePowerStateToPriv(ON_STATE
); // set a clamp until we sleep
244 registerPrioritySleepWakeInterest( &sysPowerDownHandler
, this, 0); // install power change handler
246 // Register for a notification when IODisplayWrangler is published
247 addNotification( gIOPublishNotification
, serviceMatching("IODisplayWrangler"), &displayWranglerPublished
, this, 0);
249 registerService(); // let clients find us
254 // **********************************************************************************
257 // Receive a setProperty call
258 // The "System Boot" property means the system is completely booted.
259 // **********************************************************************************
260 IOReturn
IOPMrootDomain::setProperties ( OSObject
*props_obj
)
262 OSDictionary
*dict
= OSDynamicCast(OSDictionary
, props_obj
);
264 if(!dict
) return kIOReturnBadArgument
;
266 if(dict
->getObject(OSString::withCString("System Boot Complete"))) {
267 systemBooting
= false;
268 kprintf("IOPM: received System Boot Complete property");
272 return kIOReturnSuccess
;
276 //*********************************************************************************
279 // Power Managment is informing us that we are the root power domain.
280 // We know we are not the root however, since we have just instantiated a parent
281 // for ourselves and made it the root. We override this method so it will have
283 //*********************************************************************************
284 IOReturn
IOPMrootDomain::youAreRoot ( void )
289 // **********************************************************************************
293 // **********************************************************************************
294 void IOPMrootDomain::command_received ( void * w
, void * x
, void * y
, void * z
)
296 super::command_received(w
,x
,y
,z
);
300 // **********************************************************************************
301 // broadcast_aggressiveness
303 // **********************************************************************************
304 IOReturn
broadcast_aggressiveness ( OSObject
* root
, void * x
, void * y
, void *, void * )
306 ((IOPMrootDomain
*)root
)->broadcast_it((unsigned long)x
,(unsigned long)y
);
311 // **********************************************************************************
314 // We are behind the command gate to broadcast an aggressiveness factor. We let the
315 // superclass do it, but we need to snoop on factors that affect idle sleep.
316 // **********************************************************************************
317 void IOPMrootDomain::broadcast_it (unsigned long type
, unsigned long value
)
319 super::setAggressiveness(type
,value
);
321 // Save user's spin down timer to restore after we replace it for idle sleep
322 if( type
== kPMMinutesToSpinDown
) user_spindown
= value
;
324 // Use longestNonSleepSlider to calculate dimming adjust idle sleep timer
325 longestNonSleepSlider
= pm_vars
->current_aggressiveness_values
[kPMMinutesToDim
];
328 if ( type
== kPMMinutesToSleep
) {
329 if ( (sleepSlider
== 0) && (value
!= 0) ) {
331 adjustPowerState(); // idle sleep is now enabled, maybe sleep now
334 if ( sleepSlider
== 0 ) {
335 adjustPowerState(); // idle sleep is now disabled
336 patriarch
->wakeSystem(); // make sure we're powered
339 if ( sleepSlider
> longestNonSleepSlider
) {
340 extraSleepDelay
= sleepSlider
- longestNonSleepSlider
;
348 // **********************************************************************************
351 // **********************************************************************************
352 static void sleepTimerExpired ( thread_call_param_t us
)
354 ((IOPMrootDomain
*)us
)->handleSleepTimerExpiration();
358 static void wakeupClamshellTimerExpired ( thread_call_param_t us
)
360 ((IOPMrootDomain
*)us
)->stopIgnoringClamshellEventsDuringWakeup();
364 // **********************************************************************************
365 // handleSleepTimerExpiration
367 // The time between the sleep idle timeout and the next longest one has elapsed.
368 // It's time to sleep. Start that by removing the clamp that's holding us awake.
369 // **********************************************************************************
370 void IOPMrootDomain::handleSleepTimerExpiration ( void )
372 // accelerate disk spin down if spin down timer is non-zero (zero = never spin down)
373 if(0 != user_spindown
)
374 setQuickSpinDownTimeout();
381 void IOPMrootDomain::stopIgnoringClamshellEventsDuringWakeup(void)
385 // Allow clamshell-induced sleep now
386 ignoringClamshellDuringWakeup
= false;
388 if ((state
= getProperty(kAppleClamshellStateKey
)))
389 publishResource(kAppleClamshellStateKey
, state
);
392 //*********************************************************************************
395 // Some aggressiveness factor has changed. We broadcast it to the hierarchy while on
396 // the Power Mangement workloop thread. This enables objects in the
397 // hierarchy to successfully alter their idle timers, which are all on the
399 //*********************************************************************************
401 IOReturn
IOPMrootDomain::setAggressiveness ( unsigned long type
, unsigned long newLevel
)
403 if ( pm_vars
->PMcommandGate
) {
404 pm_vars
->PMcommandGate
->runAction(broadcast_aggressiveness
,(void *)type
,(void *)newLevel
);
407 return kIOReturnSuccess
;
411 // **********************************************************************************
414 // **********************************************************************************
415 IOReturn
IOPMrootDomain::sleepSystem ( void )
417 kprintf("sleep demand received\n");
418 if ( !systemBooting
&& allowSleep
&& sleepIsSupported
) {
419 patriarch
->sleepSystem();
420 return kIOReturnSuccess
;
422 if ( !systemBooting
&& allowSleep
&& !sleepIsSupported
) {
423 patriarch
->dozeSystem();
424 return kIOReturnSuccess
;
426 return kIOReturnSuccess
;
430 // **********************************************************************************
433 // **********************************************************************************
434 IOReturn
IOPMrootDomain::shutdownSystem ( void )
436 patriarch
->shutDownSystem();
437 return kIOReturnSuccess
;
441 // **********************************************************************************
444 // **********************************************************************************
445 IOReturn
IOPMrootDomain::restartSystem ( void )
447 patriarch
->restartSystem();
448 return kIOReturnSuccess
;
452 // **********************************************************************************
455 // This overrides powerChangeDone in IOService.
457 // Finder sleep and idle sleep move us from the ON state to the SLEEP_STATE.
459 // If we just finished going to the SLEEP_STATE, and the platform is capable of true sleep,
460 // sleep the kernel. Otherwise switch up to the DOZE_STATE which will keep almost
461 // everything as off as it can get.
463 // **********************************************************************************
464 void IOPMrootDomain::powerChangeDone ( unsigned long previousState
)
466 OSNumber
* propertyPtr
;
467 unsigned short theProperty
;
468 AbsoluteTime deadline
;
470 switch ( pm_vars
->myCurrentState
) {
472 if ( canSleep
&& sleepIsSupported
) {
473 idleSleepPending
= false; // re-enable this timer for next sleep
474 IOLog("System Sleep\n");
475 pm_vars
->thePlatform
->sleepKernel(); // sleep now
477 ioSPMTrace(IOPOWER_WAKE
, * (int *) this); // now we're waking
479 clock_interval_to_deadline(30, kSecondScale
, &deadline
); // stay awake for at least 30 seconds
480 thread_call_enter_delayed(extraSleepTimer
, deadline
);
481 idleSleepPending
= true; // this gets turned off when we sleep again
483 // Ignore closed clamshell during wakeup and for a few seconds
484 // after wakeup is complete
485 ignoringClamshellDuringWakeup
= true;
487 gSleepOrShutdownPending
= 0; // sleep transition complete
488 patriarch
->wakeSystem(); // get us some power
490 IOLog("System Wake\n");
491 systemWake(); // tell the tree we're waking
493 // Allow drivers to request extra processing time before clamshell
494 // sleep if kIOREMSleepEnabledKey is present.
495 // Ignore clamshell events for at least 5 seconds
496 if(getProperty(kIOREMSleepEnabledKey
)) {
497 // clamshellWakeupIgnore callout clears ignoreClamshellDuringWakeup bit
498 clock_interval_to_deadline(5, kSecondScale
, &deadline
);
499 if(clamshellWakeupIgnore
) thread_call_enter_delayed(clamshellWakeupIgnore
, deadline
);
500 } else ignoringClamshellDuringWakeup
= false;
502 propertyPtr
= OSDynamicCast(OSNumber
,getProperty("WakeEvent"));
503 if ( propertyPtr
) { // find out what woke us
504 theProperty
= propertyPtr
->unsigned16BitValue();
505 IOLog("Wake event %04x\n",theProperty
);
506 if ( (theProperty
& 0x0008) || //lid
507 (theProperty
& 0x0800) || // front panel button
508 (theProperty
& 0x0020) || // external keyboard
509 (theProperty
& 0x0001) ) { // internal keyboard
514 IOLog("Unknown wake event\n");
515 reportUserInput(); // don't know, call it user input then
518 changePowerStateToPriv(ON_STATE
); // wake for thirty seconds
519 powerOverrideOffPriv();
522 patriarch
->sleepToDoze(); // allow us to step up a power state
523 changePowerStateToPriv(DOZE_STATE
); // and do it
528 if ( previousState
!= DOZE_STATE
) {
529 IOLog("System Doze\n");
531 idleSleepPending
= false; // re-enable this timer for next sleep
532 gSleepOrShutdownPending
= 0;
536 IOLog("System Restart\n");
537 PEHaltRestart(kPERestartCPU
);
541 IOLog("System Halt\n");
542 PEHaltRestart(kPEHaltCPU
);
548 // **********************************************************************************
551 // The Display Wrangler calls here when it switches to its highest state. If the
552 // system is currently dozing, allow it to wake by making sure the parent is
554 // **********************************************************************************
555 void IOPMrootDomain::wakeFromDoze( void )
557 if ( pm_vars
->myCurrentState
== DOZE_STATE
) {
558 canSleep
= true; // reset this till next attempt
559 powerOverrideOffPriv();
560 patriarch
->wakeSystem(); // allow us to wake if children so desire
565 // **********************************************************************************
568 // Adds a new feature to the supported features dictionary
571 // **********************************************************************************
572 void IOPMrootDomain::publishFeature( const char * feature
)
574 OSDictionary
*features
= (OSDictionary
*)getProperty(kRootDomainSupportedFeatures
);
576 features
->setObject(feature
, kOSBooleanTrue
);
580 // **********************************************************************************
583 // **********************************************************************************
584 IOReturn
IOPMrootDomain::newUserClient( task_t owningTask
, void * /* security_id */, UInt32 type
, IOUserClient
** handler
)
586 IOReturn err
= kIOReturnSuccess
;
587 RootDomainUserClient
* client
;
589 client
= RootDomainUserClient::withTask(owningTask
);
591 if( !client
|| (false == client
->attach( this )) ||
592 (false == client
->start( this )) ) {
594 client
->detach( this );
598 err
= kIOReturnNoMemory
;
604 //*********************************************************************************
605 // receivePowerNotification
607 // The power controller is notifying us of a hardware-related power management
608 // event that we must handle. This is a result of an 'environment' interrupt from
609 // the power mgt micro.
610 //*********************************************************************************
612 IOReturn
IOPMrootDomain::receivePowerNotification (UInt32 msg
)
614 if (msg
& kIOPMOverTemp
) {
615 IOLog("Power Management received emergency overtemp signal. Going to sleep.");
616 (void) sleepSystem ();
618 if (msg
& kIOPMSetDesktopMode
) {
619 desktopMode
= (0 != (msg
& kIOPMSetValue
));
620 msg
&= ~(kIOPMSetDesktopMode
| kIOPMSetValue
);
622 if (msg
& kIOPMSetACAdaptorConnected
) {
623 acAdaptorConnect
= (0 != (msg
& kIOPMSetValue
));
624 msg
&= ~(kIOPMSetACAdaptorConnected
| kIOPMSetValue
);
626 if (msg
& kIOPMEnableClamshell
) {
627 ignoringClamshell
= false;
629 if (msg
& kIOPMDisableClamshell
) {
630 ignoringClamshell
= true;
633 if (msg
& kIOPMProcessorSpeedChange
) {
634 IOService
*pmu
= waitForService(serviceMatching("ApplePMU"));
635 pmu
->callPlatformFunction("prepareForSleep", false, 0, 0, 0, 0);
636 pm_vars
->thePlatform
->sleepKernel();
637 pmu
->callPlatformFunction("recoverFromSleep", false, 0, 0, 0, 0);
640 if (msg
& kIOPMSleepNow
) {
641 (void) sleepSystem ();
644 if (msg
& kIOPMPowerEmergency
) {
645 (void) sleepSystem ();
648 if (msg
& kIOPMClamshellClosed
) {
649 if ( !ignoringClamshell
&& !ignoringClamshellDuringWakeup
650 && (!desktopMode
|| !acAdaptorConnect
) ) {
652 (void) sleepSystem ();
656 if (msg
& kIOPMPowerButton
) { // toggle state of sleep/wake
657 if ( pm_vars
->myCurrentState
== DOZE_STATE
) { // are we dozing?
658 systemWake(); // yes, tell the tree we're waking
659 reportUserInput(); // wake the Display Wrangler
662 (void) sleepSystem ();
666 // if the case has been closed, we allow
667 // the machine to be put to sleep or to idle sleep
669 if ( (msg
& kIOPMAllowSleep
) && !allowSleep
) {
674 // if the case has been opened, we disallow sleep/doze
676 if (msg
& kIOPMPreventSleep
) {
678 if ( pm_vars
->myCurrentState
== DOZE_STATE
) { // are we dozing?
679 systemWake(); // yes, tell the tree we're waking
681 reportUserInput(); // wake the Display Wrangler
685 patriarch
->wakeSystem(); // make sure we have power to clamp
693 //*********************************************************************************
696 //*********************************************************************************
698 void IOPMrootDomain::setSleepSupported( IOOptionBits flags
)
700 if ( flags
& kPCICantSleep
) {
704 platformSleepSupport
= flags
;
709 //*********************************************************************************
710 // requestPowerDomainState
712 // The root domain intercepts this call to the superclass.
714 // If the clamp bit is not set in the desire, then the child doesn't need the power
715 // state it's requesting; it just wants it. The root ignores desires but not needs.
716 // If the clamp bit is not set, the root takes it that the child can tolerate no
717 // power and interprets the request accordingly. If all children can thus tolerate
718 // no power, we are on our way to idle sleep.
719 //*********************************************************************************
721 IOReturn
IOPMrootDomain::requestPowerDomainState ( IOPMPowerFlags desiredState
, IOPowerConnection
* whichChild
, unsigned long specification
)
725 IOPowerConnection
* connection
;
726 unsigned long powerRequestFlag
= 0;
727 IOPMPowerFlags editedDesire
= desiredState
;
729 if ( !(desiredState
& kIOPMPreventIdleSleep
) ) { // if they don't really need it, they don't get it
734 IOLockLock(pm_vars
->childLock
); // recompute sleepIsSupported
735 // and see if all children are asleep
736 iter
= getChildIterator(gIOPowerPlane
);
737 sleepIsSupported
= true;
740 while ( (next
= iter
->getNextObject()) ) {
741 if ( (connection
= OSDynamicCast(IOPowerConnection
,next
)) ) {
742 if ( connection
== whichChild
) {
743 powerRequestFlag
+= editedDesire
;
744 if ( desiredState
& kIOPMPreventSystemSleep
) {
745 sleepIsSupported
= false;
749 powerRequestFlag
+= connection
->getDesiredDomainState();
750 if ( connection
->getPreventSystemSleepFlag() ) {
751 sleepIsSupported
= false;
759 if ( (extraSleepDelay
== 0) && (powerRequestFlag
== 0) ) {
763 adjustPowerState(); // this may put the system to sleep
765 IOLockUnlock(pm_vars
->childLock
);
767 editedDesire
|= desiredState
& kIOPMPreventSystemSleep
;
769 return super::requestPowerDomainState(editedDesire
,whichChild
,specification
);
773 //*********************************************************************************
776 //*********************************************************************************
778 IOOptionBits
IOPMrootDomain::getSleepSupported( void )
780 return( platformSleepSupport
);
784 //*********************************************************************************
787 // We override the superclass implementation so we can send a different message
788 // type to the client or application being notified.
789 //*********************************************************************************
791 bool IOPMrootDomain::tellChangeDown ( unsigned long stateNum
)
793 switch ( stateNum
) {
796 return super::tellClientsWithResponse(kIOMessageSystemWillSleep
);
798 return super::tellClientsWithResponse(kIOMessageSystemWillRestart
);
800 return super::tellClientsWithResponse(kIOMessageSystemWillPowerOff
);
802 return super::tellChangeDown(stateNum
); // this shouldn't execute
806 //*********************************************************************************
809 // We override the superclass implementation so we can send a different message
810 // type to the client or application being notified.
812 // This must be idle sleep since we don't ask apps during any other power change.
813 //*********************************************************************************
815 bool IOPMrootDomain::askChangeDown ( unsigned long )
817 return super::tellClientsWithResponse(kIOMessageCanSystemSleep
);
821 //*********************************************************************************
824 // Notify registered applications and kernel clients that we are not
827 // We override the superclass implementation so we can send a different message
828 // type to the client or application being notified.
830 // This must be a vetoed idle sleep, since no other power change can be vetoed.
831 //*********************************************************************************
833 void IOPMrootDomain::tellNoChangeDown ( unsigned long )
835 return tellClients(kIOMessageSystemWillNotSleep
);
839 //*********************************************************************************
842 // Notify registered applications and kernel clients that we are raising power.
844 // We override the superclass implementation so we can send a different message
845 // type to the client or application being notified.
846 //*********************************************************************************
848 void IOPMrootDomain::tellChangeUp ( unsigned long stateNum
)
850 if ( stateNum
== ON_STATE
) {
851 return tellClients(kIOMessageSystemHasPoweredOn
);
855 //*********************************************************************************
858 //*********************************************************************************
860 void IOPMrootDomain::reportUserInput ( void )
865 iter
= getMatchingServices(serviceMatching("IODisplayWrangler"));
867 wrangler
= (IOService
*) iter
->getNextObject();
873 wrangler
->activityTickle(0,0);
876 //*********************************************************************************
877 // setQuickSpinDownTimeout
879 //*********************************************************************************
881 void IOPMrootDomain::setQuickSpinDownTimeout ( void )
883 //IOLog("setQuickSpinDownTimeout\n");
884 super::setAggressiveness((unsigned long)kPMMinutesToSpinDown
,(unsigned long)1);
887 //*********************************************************************************
888 // restoreUserSpinDownTimeout
890 //*********************************************************************************
892 void IOPMrootDomain::restoreUserSpinDownTimeout ( void )
894 super::setAggressiveness((unsigned long)kPMMinutesToSpinDown
,(unsigned long)user_spindown
);
897 //*********************************************************************************
898 // changePowerStateTo & changePowerStateToPriv
900 // Override of these methods for logging purposes.
901 //*********************************************************************************
903 IOReturn
IOPMrootDomain::changePowerStateTo ( unsigned long ordinal
)
905 ioSPMTrace(IOPOWER_ROOT
, * (int *) this, (int) true, (int) ordinal
);
907 return super::changePowerStateTo(ordinal
);
910 IOReturn
IOPMrootDomain::changePowerStateToPriv ( unsigned long ordinal
)
912 ioSPMTrace(IOPOWER_ROOT
, * (int *) this, (int) false, (int) ordinal
);
914 return super::changePowerStateToPriv(ordinal
);
918 //*********************************************************************************
919 // sysPowerDownHandler
921 // Receives a notification when the RootDomain changes state.
923 // Allows us to take action on system sleep, power down, and restart after
924 // applications have received their power change notifications and replied,
925 // but before drivers have powered down. We perform a vfs sync on power down.
926 //*********************************************************************************
928 IOReturn
IOPMrootDomain::sysPowerDownHandler( void * target
, void * refCon
,
929 UInt32 messageType
, IOService
* service
,
930 void * messageArgument
, vm_size_t argSize
)
933 IOPowerStateChangeNotification
* params
= (IOPowerStateChangeNotification
*) messageArgument
;
934 IOPMrootDomain
* rootDomain
= OSDynamicCast(IOPMrootDomain
, service
);
937 return kIOReturnUnsupported
;
939 switch (messageType
) {
940 case kIOMessageSystemWillSleep
:
941 rootDomain
->powerOverrideOnPriv(); // start ignoring children's requests
942 // (fall through to other cases)
944 // Interested applications have been notified of an impending power
945 // change and have acked (when applicable).
946 // This is our chance to save whatever state we can before powering
948 // We call sync_internal defined in xnu/bsd/vfs/vfs_syscalls.c,
951 // We will ack within 20 seconds
952 params
->returnValue
= 20 * 1000 * 1000;
954 if ( ! OSCompareAndSwap( 0, 1, &gSleepOrShutdownPending
) )
956 // Purposely delay the ack and hope that shutdown occurs quickly.
957 // Another option is not to schedule the thread and wait for
959 AbsoluteTime deadline
;
960 clock_interval_to_deadline( 30, kSecondScale
, &deadline
);
961 thread_call_enter1_delayed( rootDomain
->diskSyncCalloutEntry
,
962 (thread_call_param_t
)params
->powerRef
,
966 thread_call_enter1(rootDomain
->diskSyncCalloutEntry
, (thread_call_param_t
)params
->powerRef
);
967 ret
= kIOReturnSuccess
;
970 case kIOMessageSystemWillPowerOff
:
971 case kIOMessageSystemWillRestart
:
972 ret
= kIOReturnUnsupported
;
976 ret
= kIOReturnUnsupported
;
982 //*********************************************************************************
983 // displayWranglerNotification
985 // Receives a notification when the IODisplayWrangler changes state.
987 // Allows us to take action on display dim/undim.
989 // When the display goes dim we:
990 // - Start the idle sleep timer
991 // - set the quick spin down timeout
993 // On wake from display dim:
994 // - Cancel the idle sleep timer
995 // - restore the user's chosen spindown timer from the "quick" spin down value
996 //*********************************************************************************
998 IOReturn
IOPMrootDomain::displayWranglerNotification( void * target
, void * refCon
,
999 UInt32 messageType
, IOService
* service
,
1000 void * messageArgument
, vm_size_t argSize
)
1002 IOPMrootDomain
* rootDomain
= OSDynamicCast(IOPMrootDomain
, (IOService
*)target
);
1003 AbsoluteTime deadline
;
1004 static bool deviceAlreadyPoweredOff
= false;
1007 return kIOReturnUnsupported
;
1009 switch (messageType
) {
1010 case kIOMessageDeviceWillPowerOff
:
1011 // The IODisplayWrangler has powered off either because of idle display sleep
1012 // or force system sleep.
1014 // The display wrangler will send the DeviceWillPowerOff message 4 times until
1015 // it gets into its lowest state. We only want to act on the first of those 4.
1016 if( deviceAlreadyPoweredOff
) return kIOReturnUnsupported
;
1018 deviceAlreadyPoweredOff
= true;
1020 if( rootDomain
->extraSleepDelay
) {
1022 // start the extra sleep timer
1023 clock_interval_to_deadline(rootDomain
->extraSleepDelay
*60, kSecondScale
, &deadline
);
1024 thread_call_enter_delayed(rootDomain
->extraSleepTimer
, deadline
);
1025 rootDomain
->idleSleepPending
= true;
1029 // accelerate disk spin down if spin down timer is non-zero (zero = never spin down)
1030 // and if system sleep is non-Never
1031 if( (0 != rootDomain
->user_spindown
) && (0 != rootDomain
->sleepSlider
) )
1032 rootDomain
->setQuickSpinDownTimeout();
1037 case kIOMessageDeviceHasPoweredOn
:
1039 // The display has powered on either because of UI activity or wake from sleep/doze
1040 deviceAlreadyPoweredOff
= false;
1041 rootDomain
->adjustPowerState();
1044 // cancel any pending idle sleep
1045 if(rootDomain
->idleSleepPending
) {
1046 thread_call_cancel(rootDomain
->extraSleepTimer
);
1047 rootDomain
->idleSleepPending
= false;
1050 // Change the spindown value back to the user's selection from our accelerated setting
1051 if(0 != rootDomain
->user_spindown
)
1052 rootDomain
->restoreUserSpinDownTimeout();
1054 // Put on the policy maker's on clamp.
1061 return kIOReturnUnsupported
;
1064 //*********************************************************************************
1065 // displayWranglerPublished
1067 // Receives a notification when the IODisplayWrangler is published.
1068 // When it's published we install a power state change handler.
1070 //*********************************************************************************
1072 bool IOPMrootDomain::displayWranglerPublished( void * target
, void * refCon
,
1073 IOService
* newService
)
1075 IOPMrootDomain
* rootDomain
= OSDynamicCast(IOPMrootDomain
, (IOService
*)target
);
1080 rootDomain
->wrangler
= newService
;
1082 // we found the display wrangler, now install a handler
1083 if( !rootDomain
->wrangler
->registerInterest( gIOGeneralInterest
, &displayWranglerNotification
, target
, 0) ) {
1084 IOLog("IOPMrootDomain::displayWranglerPublished registerInterest failed\n");
1092 //*********************************************************************************
1095 // Some condition that affects our wake/sleep/doze decision has changed.
1097 // If the sleep slider is in the off position, we cannot sleep or doze.
1098 // If the enclosure is open, we cannot sleep or doze.
1099 // If the system is still booting, we cannot sleep or doze.
1101 // In those circumstances, we prevent sleep and doze by holding power on with
1102 // changePowerStateToPriv(ON).
1104 // If the above conditions do not exist, and also the sleep timer has expired, we
1105 // allow sleep or doze to occur with either changePowerStateToPriv(SLEEP) or
1106 // changePowerStateToPriv(DOZE) depending on whether or not we already know the
1107 // platform cannot sleep.
1109 // In this case, sleep or doze will either occur immediately or at the next time
1110 // that no children are holding the system out of idle sleep via the
1111 // kIOPMPreventIdleSleep flag in their power state arrays.
1112 //*********************************************************************************
1114 void IOPMrootDomain::adjustPowerState( void )
1116 if ( (sleepSlider
== 0) ||
1119 changePowerStateToPriv(ON_STATE
);
1124 if ( sleepIsSupported
) {
1125 changePowerStateToPriv(SLEEP_STATE
);
1128 changePowerStateToPriv(DOZE_STATE
);
1135 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1138 #define super IOService
1140 OSDefineMetaClassAndStructors(IORootParent
, IOService
)
1142 // This array exactly parallels the state array for the root domain.
1143 // Power state changes initiated by a device can be vetoed by a client of the device, and
1144 // power state changes initiated by the parent of a device cannot be vetoed by a client of the device,
1145 // so when the root domain wants a power state change that cannot be vetoed (e.g. demand sleep), it asks
1146 // its parent to make the change. That is the reason for this complexity.
1148 static IOPMPowerState patriarchPowerStates
[number_of_power_states
] = {
1149 {1,0,0,0,0,0,0,0,0,0,0,0}, // off
1150 {1,0,RESTART_POWER
,0,0,0,0,0,0,0,0,0}, // reset
1151 {1,0,SLEEP_POWER
,0,0,0,0,0,0,0,0,0}, // sleep
1152 {1,0,DOZE_POWER
,0,0,0,0,0,0,0,0,0}, // doze
1153 {1,0,ON_POWER
,0,0,0,0,0,0,0,0,0} // running
1156 bool IORootParent::start ( IOService
* nub
)
1158 mostRecentChange
= ON_STATE
;
1161 registerPowerDriver(this,patriarchPowerStates
,number_of_power_states
);
1162 powerOverrideOnPriv();
1167 void IORootParent::shutDownSystem ( void )
1169 mostRecentChange
= OFF_STATE
;
1170 changePowerStateToPriv(OFF_STATE
);
1174 void IORootParent::restartSystem ( void )
1176 mostRecentChange
= RESTART_STATE
;
1177 changePowerStateToPriv(RESTART_STATE
);
1181 void IORootParent::sleepSystem ( void )
1183 mostRecentChange
= SLEEP_STATE
;
1184 changePowerStateToPriv(SLEEP_STATE
);
1188 void IORootParent::dozeSystem ( void )
1190 mostRecentChange
= DOZE_STATE
;
1191 changePowerStateToPriv(DOZE_STATE
);
1194 // Called in demand sleep when sleep discovered to be impossible after actually attaining that state.
1195 // This brings the parent to doze, which allows the root to step up from sleep to doze.
1197 // In idle sleep, do nothing because the parent is still on and the root can freely change state.
1199 void IORootParent::sleepToDoze ( void )
1201 if ( mostRecentChange
== SLEEP_STATE
) {
1202 changePowerStateToPriv(DOZE_STATE
);
1207 void IORootParent::wakeSystem ( void )
1209 mostRecentChange
= ON_STATE
;
1210 changePowerStateToPriv(ON_STATE
);