2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
22 #include <IOKit/IOWorkLoop.h>
23 #include <IOKit/IOCommandGate.h>
24 #include <IOKit/IOTimerEventSource.h>
25 #include <IOKit/IOPlatformExpert.h>
26 #include <IOKit/IOKitDebug.h>
27 #include <IOKit/IOTimeStamp.h>
28 #include <IOKit/pwr_mgt/RootDomain.h>
29 #include <IOKit/pwr_mgt/IOPMPrivate.h>
30 #include <IOKit/IOMessage.h>
31 #include "RootDomainUserClient.h"
32 #include "IOKit/pwr_mgt/IOPowerConnection.h"
33 #include "IOPMPowerStateQueue.h"
35 extern "C" void kprintf(const char *, ...);
37 extern const IORegistryPlane
* gIOPowerPlane
;
39 // debug trace function
41 ioSPMTrace(unsigned int csc
,
42 unsigned int a
= 0, unsigned int b
= 0,
43 unsigned int c
= 0, unsigned int d
= 0)
45 if (gIOKitDebug
& kIOLogTracePower
)
46 IOTimeStampConstant(IODBG_POWER(csc
), a
, b
, c
, d
);
49 IOReturn
broadcast_aggressiveness ( OSObject
*, void *, void *, void *, void * );
50 static void sleepTimerExpired(thread_call_param_t
);
51 static void wakeupClamshellTimerExpired ( thread_call_param_t us
);
54 #define number_of_power_states 5
56 #define RESTART_STATE 1
61 #define ON_POWER kIOPMPowerOn
62 #define RESTART_POWER kIOPMRestart
63 #define SLEEP_POWER kIOPMAuxPowerOn
64 #define DOZE_POWER kIOPMDoze
66 static IOPMPowerState ourPowerStates
[number_of_power_states
] = {
67 {1,0, 0, 0,0,0,0,0,0,0,0,0}, // state 0, off
68 {1,kIOPMRestartCapability
, kIOPMRestart
, RESTART_POWER
,0,0,0,0,0,0,0,0}, // state 1, restart
69 {1,kIOPMSleepCapability
, kIOPMSleep
, SLEEP_POWER
,0,0,0,0,0,0,0,0}, // state 2, sleep
70 {1,kIOPMDoze
, kIOPMDoze
, DOZE_POWER
,0,0,0,0,0,0,0,0}, // state 3, doze
71 {1,kIOPMPowerOn
, kIOPMPowerOn
, ON_POWER
,0,0,0,0,0,0,0,0}, // state 4, on
74 // RESERVED IOPMrootDomain class variables
75 #define diskSyncCalloutEntry _reserved->diskSyncCalloutEntry
76 #define _settingController _reserved->_settingController
78 static IOPMrootDomain
* gRootDomain
;
79 static UInt32 gSleepOrShutdownPending
= 0;
82 #define super IOService
83 OSDefineMetaClassAndStructors(IOPMrootDomain
,IOService
)
87 IONotifier
* registerSleepWakeInterest(IOServiceInterestHandler handler
, void * self
, void * ref
= 0)
89 return gRootDomain
->registerInterest( gIOGeneralInterest
, handler
, self
, ref
);
92 IONotifier
* registerPrioritySleepWakeInterest(IOServiceInterestHandler handler
, void * self
, void * ref
= 0)
94 return gRootDomain
->registerInterest( gIOPriorityPowerStateInterest
, handler
, self
, ref
);
97 IOReturn
acknowledgeSleepWakeNotification(void * PMrefcon
)
99 return gRootDomain
->allowPowerChange ( (unsigned long)PMrefcon
);
102 IOReturn
vetoSleepWakeNotification(void * PMrefcon
)
104 return gRootDomain
->cancelPowerChange ( (unsigned long)PMrefcon
);
107 IOReturn
rootDomainRestart ( void )
109 return gRootDomain
->restartSystem();
112 IOReturn
rootDomainShutdown ( void )
114 return gRootDomain
->shutdownSystem();
117 void IOSystemShutdownNotification ( void )
119 for ( int i
= 0; i
< 100; i
++ )
121 if ( OSCompareAndSwap( 0, 1, &gSleepOrShutdownPending
) ) break;
126 int sync_internal(void);
130 A device is always in the highest power state which satisfies its driver, its policy-maker, and any power domain
131 children it has, but within the constraint of the power state provided by its parent. The driver expresses its desire by
132 calling changePowerStateTo(), the policy-maker expresses its desire by calling changePowerStateToPriv(), and the children
133 express their desires by calling requestPowerDomainState().
135 The Root Power Domain owns the policy for idle and demand sleep and doze for the system. It is a power-managed IOService just
136 like the others in the system. It implements several power states which correspond to what we see as Sleep, Doze, etc.
138 The sleep/doze policy is as follows:
139 Sleep and Doze are prevented if the case is open so that nobody will think the machine is off and plug/unplug cards.
140 Sleep and Doze are prevented if the sleep timeout slider in the preferences panel is at zero.
141 The system cannot Sleep, but can Doze if some object in the tree is in a power state marked kIOPMPreventSystemSleep.
143 These three conditions are enforced using the "driver clamp" by calling changePowerStateTo(). For example, if the case is
144 opened, changePowerStateTo(ON_STATE) is called to hold the system on regardless of the desires of the children of the root or
145 the state of the other clamp.
147 Demand Sleep/Doze is initiated by pressing the front panel power button, closing the clamshell, or selecting the menu item.
148 In this case the root's parent actually initiates the power state change so that the root has no choice and does not give
149 applications the opportunity to veto the change.
151 Idle Sleep/Doze occurs if no objects in the tree are in a state marked kIOPMPreventIdleSleep. When this is true, the root's
152 children are not holding the root on, so it sets the "policy-maker clamp" by calling changePowerStateToPriv(ON_STATE)
153 to hold itself on until the sleep timer expires. This timer is set for the difference between the sleep timeout slider and
154 the larger of the display dim timeout slider and the disk spindown timeout slider in the Preferences panel. For example, if
155 the system is set to sleep after thirty idle minutes, and the display and disk are set to sleep after five idle minutes,
156 when there is no longer an object in the tree holding the system out of Idle Sleep (via kIOPMPreventIdleSleep), the root
157 sets its timer for 25 minutes (30 - 5). When the timer expires, it releases its clamp and now nothing is holding it awake,
160 Demand sleep is prevented when the system is booting. When preferences are transmitted by the loginwindow at the end of
161 boot, a flag is cleared, and this allows subsequent Demand Sleep.
163 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
164 a clamp, but sets a flag which is noticed before actually sleeping the kernel. If the flag is set, the root steps up
165 one power state from Sleep to Doze, and any objects in the tree for which this is relevent will act appropriately (USB and
166 ADB will turn on again so that they can wake the system out of Doze (keyboard/mouse activity will cause the Display Wrangler
170 // **********************************************************************************
172 IOPMrootDomain
* IOPMrootDomain::construct( void )
174 IOPMrootDomain
*root
;
176 root
= new IOPMrootDomain
;
183 // **********************************************************************************
185 static void disk_sync_callout(thread_call_param_t p0
, thread_call_param_t p1
)
187 IOService
*rootDomain
= (IOService
*) p0
;
188 unsigned long pmRef
= (unsigned long) p1
;
191 rootDomain
->allowPowerChange(pmRef
);
194 // **********************************************************************************
197 // We don't do much here. The real initialization occurs when the platform
198 // expert informs us we are the root.
199 // **********************************************************************************
202 bool IOPMrootDomain::start ( IOService
* nub
)
204 OSDictionary
*tmpDict
;
206 pmPowerStateQueue
= 0;
208 _reserved
= (ExpansionData
*)IOMalloc(sizeof(ExpansionData
));
209 if(!_reserved
) return false;
216 setProperty("IOSleepSupported","");
218 sleepIsSupported
= true;
219 systemBooting
= true;
220 ignoringClamshell
= true;
222 idleSleepPending
= false;
226 _settingController
= NULL
;
227 ignoringClamshellDuringWakeup
= false;
229 tmpDict
= OSDictionary::withCapacity(1);
230 setProperty(kRootDomainSupportedFeatures
, tmpDict
);
233 pm_vars
->PMworkloop
= IOWorkLoop::workLoop();
234 pmPowerStateQueue
= IOPMPowerStateQueue::PMPowerStateQueue(this);
235 pm_vars
->PMworkloop
->addEventSource(pmPowerStateQueue
);
237 extraSleepTimer
= thread_call_allocate((thread_call_func_t
)sleepTimerExpired
, (thread_call_param_t
) this);
238 clamshellWakeupIgnore
= thread_call_allocate((thread_call_func_t
)wakeupClamshellTimerExpired
, (thread_call_param_t
) this);
239 diskSyncCalloutEntry
= thread_call_allocate(&disk_sync_callout
, (thread_call_param_t
) this);
242 patriarch
= new IORootParent
;
244 patriarch
->attach(this);
245 patriarch
->start(this);
246 patriarch
->youAreRoot();
247 patriarch
->wakeSystem();
248 patriarch
->addPowerChild(this);
250 registerPowerDriver(this,ourPowerStates
,number_of_power_states
);
252 setPMRootDomain(this);
253 // set a clamp until we sleep
254 changePowerStateToPriv(ON_STATE
);
256 // install power change handler
257 registerPrioritySleepWakeInterest( &sysPowerDownHandler
, this, 0);
259 // Register for a notification when IODisplayWrangler is published
260 addNotification( gIOPublishNotification
, serviceMatching("IODisplayWrangler"), &displayWranglerPublished
, this, 0);
262 const OSSymbol
*ucClassName
= OSSymbol::withCStringNoCopy("RootDomainUserClient");
263 setProperty(gIOUserClientClassKey
, (OSMetaClassBase
*) ucClassName
);
264 ucClassName
->release();
266 registerService(); // let clients find us
271 IOReturn
IOPMrootDomain::setPMSetting(int type
, OSNumber
*n
)
273 if(_settingController
&& _settingController
->func
) {
275 seconds
= n
->unsigned32BitValue();
276 return (*(_settingController
->func
))(type
, seconds
, _settingController
->refcon
);
278 return kIOReturnNoDevice
;
282 // **********************************************************************************
285 // Receive a setProperty call
286 // The "System Boot" property means the system is completely booted.
287 // **********************************************************************************
288 IOReturn
IOPMrootDomain::setProperties ( OSObject
*props_obj
)
290 IOReturn return_value
= kIOReturnSuccess
;
291 OSDictionary
*dict
= OSDynamicCast(OSDictionary
, props_obj
);
294 OSString
*boot_complete_string
= OSString::withCString("System Boot Complete");
295 OSString
*power_button_string
= OSString::withCString("DisablePowerButtonSleep");
296 OSString
*stall_halt_string
= OSString::withCString("StallSystemAtHalt");
297 OSString
*auto_wake_string
= OSString::withCString("wake");
298 OSString
*auto_power_string
= OSString::withCString("poweron");
299 OSString
*wakeonring_string
= OSString::withCString("WakeOnRing");
300 OSString
*fileserver_string
= OSString::withCString("AutoRestartOnPowerLoss");
301 OSString
*wakeonlid_string
= OSString::withCString("WakeOnLid");
302 OSString
*wakeonac_string
= OSString::withCString("WakeOnACChange");
306 return_value
= kIOReturnBadArgument
;
311 && boot_complete_string
312 && dict
->getObject(boot_complete_string
))
314 systemBooting
= false;
318 if( power_button_string
319 && (b
= OSDynamicCast(OSBoolean
, dict
->getObject(power_button_string
))) )
321 setProperty(power_button_string
, b
);
324 if( stall_halt_string
325 && (b
= OSDynamicCast(OSBoolean
, dict
->getObject(stall_halt_string
))) )
327 setProperty(stall_halt_string
, b
);
330 // Relay AutoWake setting to its controller
332 && (n
= OSDynamicCast(OSNumber
, dict
->getObject(auto_wake_string
))) )
334 return_value
= setPMSetting(kIOPMAutoWakeSetting
, n
);
335 if(kIOReturnSuccess
!= return_value
) goto exit
;
338 // Relay AutoPower setting to its controller
339 if( auto_power_string
340 && (n
= OSDynamicCast(OSNumber
, dict
->getObject(auto_power_string
))) )
342 return_value
= setPMSetting(kIOPMAutoPowerOnSetting
, n
);
343 if(kIOReturnSuccess
!= return_value
) goto exit
;
346 // Relay WakeOnRing setting to its controller
347 if( wakeonring_string
348 && (n
= OSDynamicCast(OSNumber
, dict
->getObject(wakeonring_string
))) )
350 return_value
= setPMSetting(kIOPMWakeOnRingSetting
, n
);
351 if(kIOReturnSuccess
!= return_value
) goto exit
;
354 // Relay FileServer setting to its controller
355 if( fileserver_string
356 && (n
= OSDynamicCast(OSNumber
, dict
->getObject(fileserver_string
))) )
358 return_value
= setPMSetting(kIOPMAutoRestartOnPowerLossSetting
, n
);
359 if(kIOReturnSuccess
!= return_value
) goto exit
;
362 // Relay WakeOnLid setting to its controller
364 && (n
= OSDynamicCast(OSNumber
, dict
->getObject(wakeonlid_string
))) )
366 return_value
= setPMSetting(kIOPMWakeOnLidSetting
, n
);
367 if(kIOReturnSuccess
!= return_value
) goto exit
;
370 // Relay WakeOnACChange setting to its controller
372 && (n
= OSDynamicCast(OSNumber
, dict
->getObject(wakeonac_string
))) )
374 return_value
= setPMSetting(kIOPMWakeOnACChangeSetting
, n
);
375 if(kIOReturnSuccess
!= return_value
) goto exit
;
380 if(boot_complete_string
) boot_complete_string
->release();
381 if(power_button_string
) power_button_string
->release();
382 if(stall_halt_string
) stall_halt_string
->release();
383 if(auto_wake_string
) auto_wake_string
->release();
384 if(auto_power_string
) auto_power_string
->release();
385 if(wakeonring_string
) wakeonring_string
->release();
386 if(fileserver_string
) fileserver_string
->release();
387 if(wakeonlid_string
) wakeonlid_string
->release();
388 if(wakeonac_string
) wakeonac_string
->release();
393 //*********************************************************************************
396 // Power Managment is informing us that we are the root power domain.
397 // We know we are not the root however, since we have just instantiated a parent
398 // for ourselves and made it the root. We override this method so it will have
400 //*********************************************************************************
401 IOReturn
IOPMrootDomain::youAreRoot ( void )
406 // **********************************************************************************
410 // **********************************************************************************
411 void IOPMrootDomain::command_received ( void * w
, void * x
, void * y
, void * z
)
413 super::command_received(w
,x
,y
,z
);
417 // **********************************************************************************
418 // broadcast_aggressiveness
420 // **********************************************************************************
421 IOReturn
broadcast_aggressiveness ( OSObject
* root
, void * x
, void * y
, void *, void * )
423 ((IOPMrootDomain
*)root
)->broadcast_it((unsigned long)x
,(unsigned long)y
);
428 // **********************************************************************************
431 // We are behind the command gate to broadcast an aggressiveness factor. We let the
432 // superclass do it, but we need to snoop on factors that affect idle sleep.
433 // **********************************************************************************
434 void IOPMrootDomain::broadcast_it (unsigned long type
, unsigned long value
)
436 super::setAggressiveness(type
,value
);
438 // Save user's spin down timer to restore after we replace it for idle sleep
439 if( type
== kPMMinutesToSpinDown
) user_spindown
= value
;
441 // Use longestNonSleepSlider to calculate dimming adjust idle sleep timer
442 longestNonSleepSlider
= pm_vars
->current_aggressiveness_values
[kPMMinutesToDim
];
445 if ( type
== kPMMinutesToSleep
) {
446 if ( (sleepSlider
== 0) && (value
!= 0) ) {
448 // idle sleep is now enabled, maybe sleep now
452 if ( sleepSlider
== 0 ) {
453 // idle sleep is now disabled
455 // make sure we're powered
456 patriarch
->wakeSystem();
459 if ( sleepSlider
> longestNonSleepSlider
) {
460 extraSleepDelay
= sleepSlider
- longestNonSleepSlider
;
468 // **********************************************************************************
471 // **********************************************************************************
472 static void sleepTimerExpired ( thread_call_param_t us
)
474 ((IOPMrootDomain
*)us
)->handleSleepTimerExpiration();
478 static void wakeupClamshellTimerExpired ( thread_call_param_t us
)
480 ((IOPMrootDomain
*)us
)->stopIgnoringClamshellEventsDuringWakeup();
484 // **********************************************************************************
485 // handleSleepTimerExpiration
487 // The time between the sleep idle timeout and the next longest one has elapsed.
488 // It's time to sleep. Start that by removing the clamp that's holding us awake.
489 // **********************************************************************************
490 void IOPMrootDomain::handleSleepTimerExpiration ( void )
492 // accelerate disk spin down if spin down timer is non-zero (zero = never spin down)
493 if(0 != user_spindown
)
494 setQuickSpinDownTimeout();
501 void IOPMrootDomain::stopIgnoringClamshellEventsDuringWakeup(void)
505 // Allow clamshell-induced sleep now
506 ignoringClamshellDuringWakeup
= false;
508 if ((state
= getProperty(kAppleClamshellStateKey
)))
509 publishResource(kAppleClamshellStateKey
, state
);
512 //*********************************************************************************
515 // Some aggressiveness factor has changed. We broadcast it to the hierarchy while on
516 // the Power Mangement workloop thread. This enables objects in the
517 // hierarchy to successfully alter their idle timers, which are all on the
519 //*********************************************************************************
521 IOReturn
IOPMrootDomain::setAggressiveness ( unsigned long type
, unsigned long newLevel
)
523 if ( pm_vars
->PMcommandGate
) {
524 pm_vars
->PMcommandGate
->runAction(broadcast_aggressiveness
,(void *)type
,(void *)newLevel
);
527 return kIOReturnSuccess
;
531 // **********************************************************************************
534 // **********************************************************************************
535 IOReturn
IOPMrootDomain::sleepSystem ( void )
537 //kprintf("sleep demand received\n");
538 if ( !systemBooting
&& allowSleep
&& sleepIsSupported
) {
539 patriarch
->sleepSystem();
540 return kIOReturnSuccess
;
542 if ( !systemBooting
&& allowSleep
&& !sleepIsSupported
) {
543 patriarch
->dozeSystem();
544 return kIOReturnSuccess
;
546 return kIOReturnSuccess
;
550 // **********************************************************************************
553 // **********************************************************************************
554 IOReturn
IOPMrootDomain::shutdownSystem ( void )
556 //patriarch->shutDownSystem();
557 return kIOReturnUnsupported
;
561 // **********************************************************************************
564 // **********************************************************************************
565 IOReturn
IOPMrootDomain::restartSystem ( void )
567 //patriarch->restartSystem();
568 return kIOReturnUnsupported
;
572 // **********************************************************************************
575 // This overrides powerChangeDone in IOService.
577 // Finder sleep and idle sleep move us from the ON state to the SLEEP_STATE.
579 // If we just finished going to the SLEEP_STATE, and the platform is capable of true sleep,
580 // sleep the kernel. Otherwise switch up to the DOZE_STATE which will keep almost
581 // everything as off as it can get.
583 // **********************************************************************************
584 void IOPMrootDomain::powerChangeDone ( unsigned long previousState
)
586 OSNumber
* propertyPtr
;
587 unsigned short theProperty
;
588 AbsoluteTime deadline
;
590 switch ( pm_vars
->myCurrentState
) {
592 if ( canSleep
&& sleepIsSupported
)
594 // re-enable this timer for next sleep
595 idleSleepPending
= false;
596 IOLog("System Sleep\n");
597 pm_vars
->thePlatform
->sleepKernel();
599 // The CPU(s) are off at this point. When they're awakened by CPU interrupt,
600 // code will resume exeuction here.
602 // Now we're waking...
603 ioSPMTrace(IOPOWER_WAKE
, * (int *) this);
605 // stay awake for at least 30 seconds
606 clock_interval_to_deadline(30, kSecondScale
, &deadline
);
607 thread_call_enter_delayed(extraSleepTimer
, deadline
);
608 // this gets turned off when we sleep again
609 idleSleepPending
= true;
611 // Ignore closed clamshell during wakeup and for a few seconds
612 // after wakeup is complete
613 ignoringClamshellDuringWakeup
= true;
615 // sleep transition complete
616 gSleepOrShutdownPending
= 0;
618 // trip the reset of the calendar clock
619 clock_wakeup_calendar();
622 patriarch
->wakeSystem();
624 // early stage wake notification
625 tellClients(kIOMessageSystemWillPowerOn
);
627 // tell the tree we're waking
628 IOLog("System Wake\n");
631 // Allow drivers to request extra processing time before clamshell
632 // sleep if kIOREMSleepEnabledKey is present.
633 // Ignore clamshell events for at least 5 seconds
634 if(getProperty(kIOREMSleepEnabledKey
)) {
635 // clamshellWakeupIgnore callout clears ignoreClamshellDuringWakeup bit
636 clock_interval_to_deadline(5, kSecondScale
, &deadline
);
637 if(clamshellWakeupIgnore
) thread_call_enter_delayed(clamshellWakeupIgnore
, deadline
);
638 } else ignoringClamshellDuringWakeup
= false;
640 // Find out what woke us
641 propertyPtr
= OSDynamicCast(OSNumber
,getProperty("WakeEvent"));
643 theProperty
= propertyPtr
->unsigned16BitValue();
644 IOLog("Wake event %04x\n",theProperty
);
645 if ( (theProperty
& 0x0008) || //lid
646 (theProperty
& 0x0800) || // front panel button
647 (theProperty
& 0x0020) || // external keyboard
648 (theProperty
& 0x0001) ) { // internal keyboard
649 // We've identified the wakeup event as UI driven
653 // Since we can't identify the wakeup event, treat it as UI activity
657 // Wake for thirty seconds
658 changePowerStateToPriv(ON_STATE
);
659 powerOverrideOffPriv();
661 // allow us to step up a power state
662 patriarch
->sleepToDoze();
664 changePowerStateToPriv(DOZE_STATE
);
669 if ( previousState
!= DOZE_STATE
)
671 IOLog("System Doze\n");
673 // re-enable this timer for next sleep
674 idleSleepPending
= false;
675 gSleepOrShutdownPending
= 0;
679 IOLog("System Restart\n");
680 PEHaltRestart(kPERestartCPU
);
684 IOLog("System Halt\n");
685 PEHaltRestart(kPEHaltCPU
);
691 // **********************************************************************************
694 // The Display Wrangler calls here when it switches to its highest state. If the
695 // system is currently dozing, allow it to wake by making sure the parent is
697 // **********************************************************************************
698 void IOPMrootDomain::wakeFromDoze( void )
700 if ( pm_vars
->myCurrentState
== DOZE_STATE
)
702 // reset this till next attempt
704 powerOverrideOffPriv();
706 // early wake notification
707 tellClients(kIOMessageSystemWillPowerOn
);
709 // allow us to wake if children so desire
710 patriarch
->wakeSystem();
715 // **********************************************************************************
718 // Adds a new feature to the supported features dictionary
721 // **********************************************************************************
722 void IOPMrootDomain::publishFeature( const char * feature
)
724 OSDictionary
*features
= (OSDictionary
*)getProperty(kRootDomainSupportedFeatures
);
726 features
->setObject(feature
, kOSBooleanTrue
);
729 void IOPMrootDomain::unIdleDevice( IOService
*theDevice
, unsigned long theState
)
731 if(pmPowerStateQueue
)
732 pmPowerStateQueue
->unIdleOccurred(theDevice
, theState
);
735 void IOPMrootDomain::announcePowerSourceChange( void )
737 messageClients(kIOPMMessageBatteryStatusHasChanged
);
740 IOReturn
IOPMrootDomain::registerPMSettingController
741 (IOPMSettingControllerCallback func
, void *info
)
743 if(_settingController
) return kIOReturnExclusiveAccess
;
745 _settingController
= (PMSettingCtrl
*)IOMalloc(sizeof(PMSettingCtrl
));
746 if(!_settingController
) return kIOReturnNoMemory
;
748 _settingController
->func
= func
;
749 _settingController
->refcon
= info
;
750 return kIOReturnSuccess
;
754 //*********************************************************************************
755 // receivePowerNotification
757 // The power controller is notifying us of a hardware-related power management
758 // event that we must handle. This is a result of an 'environment' interrupt from
759 // the power mgt micro.
760 //*********************************************************************************
762 IOReturn
IOPMrootDomain::receivePowerNotification (UInt32 msg
)
764 if (msg
& kIOPMOverTemp
)
766 IOLog("Power Management received emergency overtemp signal. Going to sleep.");
767 (void) sleepSystem ();
769 if (msg
& kIOPMSetDesktopMode
)
771 desktopMode
= (0 != (msg
& kIOPMSetValue
));
772 msg
&= ~(kIOPMSetDesktopMode
| kIOPMSetValue
);
774 if (msg
& kIOPMSetACAdaptorConnected
)
776 acAdaptorConnect
= (0 != (msg
& kIOPMSetValue
));
777 msg
&= ~(kIOPMSetACAdaptorConnected
| kIOPMSetValue
);
779 if (msg
& kIOPMEnableClamshell
)
781 ignoringClamshell
= false;
783 if (msg
& kIOPMDisableClamshell
)
785 ignoringClamshell
= true;
788 if (msg
& kIOPMProcessorSpeedChange
)
790 IOService
*pmu
= waitForService(serviceMatching("ApplePMU"));
791 pmu
->callPlatformFunction("prepareForSleep", false, 0, 0, 0, 0);
792 pm_vars
->thePlatform
->sleepKernel();
793 pmu
->callPlatformFunction("recoverFromSleep", false, 0, 0, 0, 0);
796 if (msg
& kIOPMSleepNow
)
798 (void) sleepSystem ();
801 if (msg
& kIOPMPowerEmergency
)
803 (void) sleepSystem ();
806 if (msg
& kIOPMClamshellClosed
)
808 if ( !ignoringClamshell
&& !ignoringClamshellDuringWakeup
809 && (!desktopMode
|| !acAdaptorConnect
) )
812 (void) sleepSystem ();
816 if (msg
& kIOPMPowerButton
)
818 // toggle state of sleep/wake
820 if ( pm_vars
->myCurrentState
== DOZE_STATE
)
822 // yes, tell the tree we're waking
824 // wake the Display Wrangler
828 // Check that power button sleep is enabled
829 if(kOSBooleanTrue
!= getProperty(OSString::withCString("DisablePowerButtonSleep")))
834 // if the case has been closed, we allow
835 // the machine to be put to sleep or to idle sleep
837 if ( (msg
& kIOPMAllowSleep
) && !allowSleep
)
843 // if the case has been opened, we disallow sleep/doze
845 if (msg
& kIOPMPreventSleep
) {
848 if ( pm_vars
->myCurrentState
== DOZE_STATE
) {
849 // yes, tell the tree we're waking
852 // wake the Display Wrangler
856 // make sure we have power to clamp
857 patriarch
->wakeSystem();
865 //*********************************************************************************
868 //*********************************************************************************
870 void IOPMrootDomain::setSleepSupported( IOOptionBits flags
)
872 if ( flags
& kPCICantSleep
)
876 platformSleepSupport
= flags
;
881 //*********************************************************************************
882 // requestPowerDomainState
884 // The root domain intercepts this call to the superclass.
886 // If the clamp bit is not set in the desire, then the child doesn't need the power
887 // state it's requesting; it just wants it. The root ignores desires but not needs.
888 // If the clamp bit is not set, the root takes it that the child can tolerate no
889 // power and interprets the request accordingly. If all children can thus tolerate
890 // no power, we are on our way to idle sleep.
891 //*********************************************************************************
893 IOReturn
IOPMrootDomain::requestPowerDomainState ( IOPMPowerFlags desiredState
, IOPowerConnection
* whichChild
, unsigned long specification
)
897 IOPowerConnection
*connection
;
898 unsigned long powerRequestFlag
= 0;
899 IOPMPowerFlags editedDesire
= desiredState
;
901 // if they don't really need it, they don't get it
902 if ( !(desiredState
& kIOPMPreventIdleSleep
) ) {
907 IOLockLock(pm_vars
->childLock
);
909 // recompute sleepIsSupported and see if all children are asleep
910 iter
= getChildIterator(gIOPowerPlane
);
911 sleepIsSupported
= true;
914 while ( (next
= iter
->getNextObject()) )
916 if ( (connection
= OSDynamicCast(IOPowerConnection
,next
)) )
918 if ( connection
== whichChild
)
920 powerRequestFlag
+= editedDesire
;
921 if ( desiredState
& kIOPMPreventSystemSleep
)
923 sleepIsSupported
= false;
926 powerRequestFlag
+= connection
->getDesiredDomainState();
927 if ( connection
->getPreventSystemSleepFlag() )
929 sleepIsSupported
= false;
937 if ( (extraSleepDelay
== 0) && (powerRequestFlag
== 0) )
942 // this may put the system to sleep
945 IOLockUnlock(pm_vars
->childLock
);
947 editedDesire
|= desiredState
& kIOPMPreventSystemSleep
;
949 return super::requestPowerDomainState(editedDesire
,whichChild
,specification
);
953 //*********************************************************************************
956 //*********************************************************************************
958 IOOptionBits
IOPMrootDomain::getSleepSupported( void )
960 return( platformSleepSupport
);
964 //*********************************************************************************
967 // We override the superclass implementation so we can send a different message
968 // type to the client or application being notified.
969 //*********************************************************************************
971 bool IOPMrootDomain::tellChangeDown ( unsigned long stateNum
)
973 switch ( stateNum
) {
976 return super::tellClientsWithResponse(kIOMessageSystemWillSleep
);
978 return super::tellClientsWithResponse(kIOMessageSystemWillRestart
);
980 return super::tellClientsWithResponse(kIOMessageSystemWillPowerOff
);
982 // this shouldn't execute
983 return super::tellChangeDown(stateNum
);
987 //*********************************************************************************
990 // We override the superclass implementation so we can send a different message
991 // type to the client or application being notified.
993 // This must be idle sleep since we don't ask apps during any other power change.
994 //*********************************************************************************
996 bool IOPMrootDomain::askChangeDown ( unsigned long )
998 return super::tellClientsWithResponse(kIOMessageCanSystemSleep
);
1002 //*********************************************************************************
1005 // Notify registered applications and kernel clients that we are not
1008 // We override the superclass implementation so we can send a different message
1009 // type to the client or application being notified.
1011 // This must be a vetoed idle sleep, since no other power change can be vetoed.
1012 //*********************************************************************************
1014 void IOPMrootDomain::tellNoChangeDown ( unsigned long )
1016 return tellClients(kIOMessageSystemWillNotSleep
);
1020 //*********************************************************************************
1023 // Notify registered applications and kernel clients that we are raising power.
1025 // We override the superclass implementation so we can send a different message
1026 // type to the client or application being notified.
1027 //*********************************************************************************
1029 void IOPMrootDomain::tellChangeUp ( unsigned long stateNum
)
1031 if ( stateNum
== ON_STATE
)
1033 return tellClients(kIOMessageSystemHasPoweredOn
);
1037 //*********************************************************************************
1040 //*********************************************************************************
1042 void IOPMrootDomain::reportUserInput ( void )
1048 iter
= getMatchingServices(serviceMatching("IODisplayWrangler"));
1051 wrangler
= (IOService
*) iter
->getNextObject();
1057 wrangler
->activityTickle(0,0);
1060 //*********************************************************************************
1061 // setQuickSpinDownTimeout
1063 //*********************************************************************************
1065 void IOPMrootDomain::setQuickSpinDownTimeout ( void )
1067 super::setAggressiveness((unsigned long)kPMMinutesToSpinDown
,(unsigned long)1);
1070 //*********************************************************************************
1071 // restoreUserSpinDownTimeout
1073 //*********************************************************************************
1075 void IOPMrootDomain::restoreUserSpinDownTimeout ( void )
1077 super::setAggressiveness((unsigned long)kPMMinutesToSpinDown
,(unsigned long)user_spindown
);
1080 //*********************************************************************************
1081 // changePowerStateTo & changePowerStateToPriv
1083 // Override of these methods for logging purposes.
1084 //*********************************************************************************
1086 IOReturn
IOPMrootDomain::changePowerStateTo ( unsigned long ordinal
)
1088 ioSPMTrace(IOPOWER_ROOT
, * (int *) this, (int) true, (int) ordinal
);
1090 return super::changePowerStateTo(ordinal
);
1093 IOReturn
IOPMrootDomain::changePowerStateToPriv ( unsigned long ordinal
)
1095 ioSPMTrace(IOPOWER_ROOT
, * (int *) this, (int) false, (int) ordinal
);
1097 return super::changePowerStateToPriv(ordinal
);
1101 //*********************************************************************************
1102 // sysPowerDownHandler
1104 // Receives a notification when the RootDomain changes state.
1106 // Allows us to take action on system sleep, power down, and restart after
1107 // applications have received their power change notifications and replied,
1108 // but before drivers have powered down. We perform a vfs sync on power down.
1109 //*********************************************************************************
1111 IOReturn
IOPMrootDomain::sysPowerDownHandler( void * target
, void * refCon
,
1112 UInt32 messageType
, IOService
* service
,
1113 void * messageArgument
, vm_size_t argSize
)
1116 IOPowerStateChangeNotification
*params
= (IOPowerStateChangeNotification
*) messageArgument
;
1117 IOPMrootDomain
*rootDomain
= OSDynamicCast(IOPMrootDomain
, service
);
1120 return kIOReturnUnsupported
;
1122 switch (messageType
) {
1123 case kIOMessageSystemWillSleep
:
1124 rootDomain
->powerOverrideOnPriv(); // start ignoring children's requests
1125 // (fall through to other cases)
1127 // Interested applications have been notified of an impending power
1128 // change and have acked (when applicable).
1129 // This is our chance to save whatever state we can before powering
1131 // We call sync_internal defined in xnu/bsd/vfs/vfs_syscalls.c,
1134 // We will ack within 20 seconds
1135 params
->returnValue
= 20 * 1000 * 1000;
1137 if ( ! OSCompareAndSwap( 0, 1, &gSleepOrShutdownPending
) )
1139 // Purposely delay the ack and hope that shutdown occurs quickly.
1140 // Another option is not to schedule the thread and wait for
1142 AbsoluteTime deadline
;
1143 clock_interval_to_deadline( 30, kSecondScale
, &deadline
);
1144 thread_call_enter1_delayed( rootDomain
->diskSyncCalloutEntry
,
1145 (thread_call_param_t
)params
->powerRef
,
1149 thread_call_enter1(rootDomain
->diskSyncCalloutEntry
, (thread_call_param_t
)params
->powerRef
);
1150 ret
= kIOReturnSuccess
;
1153 case kIOMessageSystemWillPowerOff
:
1154 case kIOMessageSystemWillRestart
:
1155 ret
= kIOReturnUnsupported
;
1159 ret
= kIOReturnUnsupported
;
1165 //*********************************************************************************
1166 // displayWranglerNotification
1168 // Receives a notification when the IODisplayWrangler changes state.
1170 // Allows us to take action on display dim/undim.
1172 // When the display goes dim we:
1173 // - Start the idle sleep timer
1174 // - set the quick spin down timeout
1176 // On wake from display dim:
1177 // - Cancel the idle sleep timer
1178 // - restore the user's chosen spindown timer from the "quick" spin down value
1179 //*********************************************************************************
1181 IOReturn
IOPMrootDomain::displayWranglerNotification( void * target
, void * refCon
,
1182 UInt32 messageType
, IOService
* service
,
1183 void * messageArgument
, vm_size_t argSize
)
1185 IOPMrootDomain
* rootDomain
= OSDynamicCast(IOPMrootDomain
, (IOService
*)target
);
1186 AbsoluteTime deadline
;
1187 static bool deviceAlreadyPoweredOff
= false;
1190 return kIOReturnUnsupported
;
1192 switch (messageType
) {
1193 case kIOMessageDeviceWillPowerOff
:
1194 // The IODisplayWrangler has powered off either because of idle display sleep
1195 // or force system sleep.
1197 // The display wrangler will send the DeviceWillPowerOff message 4 times until
1198 // it gets into its lowest state. We only want to act on the first of those 4.
1199 if( deviceAlreadyPoweredOff
) return kIOReturnUnsupported
;
1201 deviceAlreadyPoweredOff
= true;
1203 if( rootDomain
->extraSleepDelay
)
1205 // start the extra sleep timer
1206 clock_interval_to_deadline(rootDomain
->extraSleepDelay
*60, kSecondScale
, &deadline
);
1207 thread_call_enter_delayed(rootDomain
->extraSleepTimer
, deadline
);
1208 rootDomain
->idleSleepPending
= true;
1210 // accelerate disk spin down if spin down timer is non-zero (zero = never spin down)
1211 // and if system sleep is non-Never
1212 if( (0 != rootDomain
->user_spindown
) && (0 != rootDomain
->sleepSlider
) )
1213 rootDomain
->setQuickSpinDownTimeout();
1218 case kIOMessageDeviceHasPoweredOn
:
1220 // The display has powered on either because of UI activity or wake from sleep/doze
1221 deviceAlreadyPoweredOff
= false;
1222 rootDomain
->adjustPowerState();
1225 // cancel any pending idle sleep
1226 if(rootDomain
->idleSleepPending
)
1228 thread_call_cancel(rootDomain
->extraSleepTimer
);
1229 rootDomain
->idleSleepPending
= false;
1232 // Change the spindown value back to the user's selection from our accelerated setting
1233 if(0 != rootDomain
->user_spindown
)
1234 rootDomain
->restoreUserSpinDownTimeout();
1236 // Put on the policy maker's on clamp.
1243 return kIOReturnUnsupported
;
1246 //*********************************************************************************
1247 // displayWranglerPublished
1249 // Receives a notification when the IODisplayWrangler is published.
1250 // When it's published we install a power state change handler.
1252 //*********************************************************************************
1254 bool IOPMrootDomain::displayWranglerPublished( void * target
, void * refCon
,
1255 IOService
* newService
)
1257 IOPMrootDomain
* rootDomain
= OSDynamicCast(IOPMrootDomain
, (IOService
*)target
);
1262 rootDomain
->wrangler
= newService
;
1264 // we found the display wrangler, now install a handler
1265 if( !rootDomain
->wrangler
->registerInterest( gIOGeneralInterest
, &displayWranglerNotification
, target
, 0) ) {
1266 IOLog("IOPMrootDomain::displayWranglerPublished registerInterest failed\n");
1274 //*********************************************************************************
1277 // Some condition that affects our wake/sleep/doze decision has changed.
1279 // If the sleep slider is in the off position, we cannot sleep or doze.
1280 // If the enclosure is open, we cannot sleep or doze.
1281 // If the system is still booting, we cannot sleep or doze.
1283 // In those circumstances, we prevent sleep and doze by holding power on with
1284 // changePowerStateToPriv(ON).
1286 // If the above conditions do not exist, and also the sleep timer has expired, we
1287 // allow sleep or doze to occur with either changePowerStateToPriv(SLEEP) or
1288 // changePowerStateToPriv(DOZE) depending on whether or not we already know the
1289 // platform cannot sleep.
1291 // In this case, sleep or doze will either occur immediately or at the next time
1292 // that no children are holding the system out of idle sleep via the
1293 // kIOPMPreventIdleSleep flag in their power state arrays.
1294 //*********************************************************************************
1296 void IOPMrootDomain::adjustPowerState( void )
1298 if ( (sleepSlider
== 0) ||
1301 changePowerStateToPriv(ON_STATE
);
1306 if ( sleepIsSupported
)
1308 changePowerStateToPriv(SLEEP_STATE
);
1310 changePowerStateToPriv(DOZE_STATE
);
1317 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1320 #define super IOService
1322 OSDefineMetaClassAndStructors(IORootParent
, IOService
)
1324 // This array exactly parallels the state array for the root domain.
1325 // Power state changes initiated by a device can be vetoed by a client of the device, and
1326 // power state changes initiated by the parent of a device cannot be vetoed by a client of the device,
1327 // so when the root domain wants a power state change that cannot be vetoed (e.g. demand sleep), it asks
1328 // its parent to make the change. That is the reason for this complexity.
1330 static IOPMPowerState patriarchPowerStates
[number_of_power_states
] = {
1331 {1,0,0,0,0,0,0,0,0,0,0,0}, // off
1332 {1,0,RESTART_POWER
,0,0,0,0,0,0,0,0,0}, // reset
1333 {1,0,SLEEP_POWER
,0,0,0,0,0,0,0,0,0}, // sleep
1334 {1,0,DOZE_POWER
,0,0,0,0,0,0,0,0,0}, // doze
1335 {1,0,ON_POWER
,0,0,0,0,0,0,0,0,0} // running
1338 bool IORootParent::start ( IOService
* nub
)
1340 mostRecentChange
= ON_STATE
;
1343 registerPowerDriver(this,patriarchPowerStates
,number_of_power_states
);
1344 powerOverrideOnPriv();
1349 void IORootParent::shutDownSystem ( void )
1351 mostRecentChange
= OFF_STATE
;
1352 changePowerStateToPriv(OFF_STATE
);
1356 void IORootParent::restartSystem ( void )
1358 mostRecentChange
= RESTART_STATE
;
1359 changePowerStateToPriv(RESTART_STATE
);
1363 void IORootParent::sleepSystem ( void )
1365 mostRecentChange
= SLEEP_STATE
;
1366 changePowerStateToPriv(SLEEP_STATE
);
1370 void IORootParent::dozeSystem ( void )
1372 mostRecentChange
= DOZE_STATE
;
1373 changePowerStateToPriv(DOZE_STATE
);
1376 // Called in demand sleep when sleep discovered to be impossible after actually attaining that state.
1377 // This brings the parent to doze, which allows the root to step up from sleep to doze.
1379 // In idle sleep, do nothing because the parent is still on and the root can freely change state.
1381 void IORootParent::sleepToDoze ( void )
1383 if ( mostRecentChange
== SLEEP_STATE
) {
1384 changePowerStateToPriv(DOZE_STATE
);
1389 void IORootParent::wakeSystem ( void )
1391 mostRecentChange
= ON_STATE
;
1392 changePowerStateToPriv(ON_STATE
);