2 * Copyright (c) 1998-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 #include <IOKit/IOWorkLoop.h>
24 #include <IOKit/IOCommandGate.h>
25 #include <IOKit/IOTimerEventSource.h>
26 #include <IOKit/IOPlatformExpert.h>
27 #include <IOKit/IOKitDebug.h>
28 #include <IOKit/IOTimeStamp.h>
29 #include <IOKit/pwr_mgt/RootDomain.h>
30 #include <IOKit/pwr_mgt/IOPMPrivate.h>
31 #include <IOKit/IODeviceTreeSupport.h>
32 #include <IOKit/IOMessage.h>
33 #include "RootDomainUserClient.h"
34 #include "IOKit/pwr_mgt/IOPowerConnection.h"
35 #include "IOPMPowerStateQueue.h"
36 #include <IOKit/IOCatalogue.h>
37 #include <IOKit/IOHibernatePrivate.h>
43 extern "C" void kprintf(const char *, ...);
45 extern const IORegistryPlane
* gIOPowerPlane
;
47 IOReturn
broadcast_aggressiveness ( OSObject
*, void *, void *, void *, void * );
48 static void sleepTimerExpired(thread_call_param_t
);
49 static void wakeupClamshellTimerExpired ( thread_call_param_t us
);
52 #define number_of_power_states 5
54 #define RESTART_STATE 1
59 #define ON_POWER kIOPMPowerOn
60 #define RESTART_POWER kIOPMRestart
61 #define SLEEP_POWER kIOPMAuxPowerOn
62 #define DOZE_POWER kIOPMDoze
64 static IOPMPowerState ourPowerStates
[number_of_power_states
] = {
65 {1,0, 0, 0,0,0,0,0,0,0,0,0}, // state 0, off
66 {1,kIOPMRestartCapability
, kIOPMRestart
, RESTART_POWER
,0,0,0,0,0,0,0,0}, // state 1, restart
67 {1,kIOPMSleepCapability
, kIOPMSleep
, SLEEP_POWER
,0,0,0,0,0,0,0,0}, // state 2, sleep
68 {1,kIOPMDoze
, kIOPMDoze
, DOZE_POWER
,0,0,0,0,0,0,0,0}, // state 3, doze
69 {1,kIOPMPowerOn
, kIOPMPowerOn
, ON_POWER
,0,0,0,0,0,0,0,0}, // state 4, on
72 // RESERVED IOPMrootDomain class variables
73 #define diskSyncCalloutEntry _reserved->diskSyncCalloutEntry
74 #define _settingController _reserved->_settingController
75 #define _batteryLocationNotifier _reserved->_batteryLocationNotifier
76 #define _displayWranglerNotifier _reserved->_displayWranglerNotifier
79 static IOPMrootDomain
* gRootDomain
;
80 static UInt32 gSleepOrShutdownPending
= 0;
83 #define super IOService
84 OSDefineMetaClassAndStructors(IOPMrootDomain
,IOService
)
88 IONotifier
* registerSleepWakeInterest(IOServiceInterestHandler handler
, void * self
, void * ref
)
90 return gRootDomain
->registerInterest( gIOGeneralInterest
, handler
, self
, ref
);
93 IONotifier
* registerPrioritySleepWakeInterest(IOServiceInterestHandler handler
, void * self
, void * ref
)
95 return gRootDomain
->registerInterest( gIOPriorityPowerStateInterest
, handler
, self
, ref
);
98 IOReturn
acknowledgeSleepWakeNotification(void * PMrefcon
)
100 return gRootDomain
->allowPowerChange ( (unsigned long)PMrefcon
);
103 IOReturn
vetoSleepWakeNotification(void * PMrefcon
)
105 return gRootDomain
->cancelPowerChange ( (unsigned long)PMrefcon
);
108 IOReturn
rootDomainRestart ( void )
110 return gRootDomain
->restartSystem();
113 IOReturn
rootDomainShutdown ( void )
115 return gRootDomain
->shutdownSystem();
118 void IOSystemShutdownNotification ( void )
120 IOCatalogue::disableExternalLinker();
121 for ( int i
= 0; i
< 100; i
++ )
123 if ( OSCompareAndSwap( 0, 1, &gSleepOrShutdownPending
) ) break;
128 int sync_internal(void);
132 A device is always in the highest power state which satisfies its driver, its policy-maker, and any power domain
133 children it has, but within the constraint of the power state provided by its parent. The driver expresses its desire by
134 calling changePowerStateTo(), the policy-maker expresses its desire by calling changePowerStateToPriv(), and the children
135 express their desires by calling requestPowerDomainState().
137 The Root Power Domain owns the policy for idle and demand sleep and doze for the system. It is a power-managed IOService just
138 like the others in the system. It implements several power states which correspond to what we see as Sleep, Doze, etc.
140 The sleep/doze policy is as follows:
141 Sleep and Doze are prevented if the case is open so that nobody will think the machine is off and plug/unplug cards.
142 Sleep and Doze are prevented if the sleep timeout slider in the preferences panel is at zero.
143 The system cannot Sleep, but can Doze if some object in the tree is in a power state marked kIOPMPreventSystemSleep.
145 These three conditions are enforced using the "driver clamp" by calling changePowerStateTo(). For example, if the case is
146 opened, changePowerStateTo(ON_STATE) is called to hold the system on regardless of the desires of the children of the root or
147 the state of the other clamp.
149 Demand Sleep/Doze is initiated by pressing the front panel power button, closing the clamshell, or selecting the menu item.
150 In this case the root's parent actually initiates the power state change so that the root has no choice and does not give
151 applications the opportunity to veto the change.
153 Idle Sleep/Doze occurs if no objects in the tree are in a state marked kIOPMPreventIdleSleep. When this is true, the root's
154 children are not holding the root on, so it sets the "policy-maker clamp" by calling changePowerStateToPriv(ON_STATE)
155 to hold itself on until the sleep timer expires. This timer is set for the difference between the sleep timeout slider and
156 the larger of the display dim timeout slider and the disk spindown timeout slider in the Preferences panel. For example, if
157 the system is set to sleep after thirty idle minutes, and the display and disk are set to sleep after five idle minutes,
158 when there is no longer an object in the tree holding the system out of Idle Sleep (via kIOPMPreventIdleSleep), the root
159 sets its timer for 25 minutes (30 - 5). When the timer expires, it releases its clamp and now nothing is holding it awake,
162 Demand sleep is prevented when the system is booting. When preferences are transmitted by the loginwindow at the end of
163 boot, a flag is cleared, and this allows subsequent Demand Sleep.
165 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
166 a clamp, but sets a flag which is noticed before actually sleeping the kernel. If the flag is set, the root steps up
167 one power state from Sleep to Doze, and any objects in the tree for which this is relevent will act appropriately (USB and
168 ADB will turn on again so that they can wake the system out of Doze (keyboard/mouse activity will cause the Display Wrangler
172 // **********************************************************************************
174 IOPMrootDomain
* IOPMrootDomain::construct( void )
176 IOPMrootDomain
*root
;
178 root
= new IOPMrootDomain
;
185 // **********************************************************************************
187 static void disk_sync_callout(thread_call_param_t p0
, thread_call_param_t p1
)
189 IOService
*rootDomain
= (IOService
*) p0
;
190 unsigned long pmRef
= (unsigned long) p1
;
192 IOHibernateSystemSleep();
194 rootDomain
->allowPowerChange(pmRef
);
197 // **********************************************************************************
200 // We don't do much here. The real initialization occurs when the platform
201 // expert informs us we are the root.
202 // **********************************************************************************
205 bool IOPMrootDomain::start ( IOService
* nub
)
207 OSDictionary
*tmpDict
;
209 pmPowerStateQueue
= 0;
211 _reserved
= (ExpansionData
*)IOMalloc(sizeof(ExpansionData
));
212 if(!_reserved
) return false;
219 setProperty("IOSleepSupported","");
222 sleepIsSupported
= true;
223 systemBooting
= true;
224 ignoringClamshell
= true;
226 idleSleepPending
= false;
230 _settingController
= NULL
;
231 ignoringClamshellDuringWakeup
= false;
233 tmpDict
= OSDictionary::withCapacity(1);
234 setProperty(kRootDomainSupportedFeatures
, tmpDict
);
237 pm_vars
->PMworkloop
= IOWorkLoop::workLoop();
238 pmPowerStateQueue
= IOPMPowerStateQueue::PMPowerStateQueue(this);
239 pm_vars
->PMworkloop
->addEventSource(pmPowerStateQueue
);
241 featuresDictLock
= IOLockAlloc();
243 extraSleepTimer
= thread_call_allocate((thread_call_func_t
)sleepTimerExpired
, (thread_call_param_t
) this);
244 clamshellWakeupIgnore
= thread_call_allocate((thread_call_func_t
)wakeupClamshellTimerExpired
, (thread_call_param_t
) this);
245 diskSyncCalloutEntry
= thread_call_allocate(&disk_sync_callout
, (thread_call_param_t
) this);
248 patriarch
= new IORootParent
;
250 patriarch
->attach(this);
251 patriarch
->start(this);
252 patriarch
->youAreRoot();
253 patriarch
->wakeSystem();
254 patriarch
->addPowerChild(this);
256 registerPowerDriver(this,ourPowerStates
,number_of_power_states
);
258 setPMRootDomain(this);
259 // set a clamp until we sleep
260 changePowerStateToPriv(ON_STATE
);
262 // install power change handler
263 registerPrioritySleepWakeInterest( &sysPowerDownHandler
, this, 0);
265 // Register for a notification when IODisplayWrangler is published
266 _displayWranglerNotifier
= addNotification( gIOPublishNotification
,
267 serviceMatching("IODisplayWrangler"),
268 &displayWranglerPublished
, this, 0);
270 _batteryLocationNotifier
= addNotification( gIOPublishNotification
,
271 resourceMatching("battery"),
272 &batteryLocationPublished
, this, this);
274 const OSSymbol
*ucClassName
= OSSymbol::withCStringNoCopy("RootDomainUserClient");
275 setProperty(gIOUserClientClassKey
, (OSObject
*) ucClassName
);
276 ucClassName
->release();
278 IORegistryEntry
*temp_entry
= NULL
;
279 if( (temp_entry
= IORegistryEntry::fromPath("mac-io/battery", gIODTPlane
)) ||
280 (temp_entry
= IORegistryEntry::fromPath("mac-io/via-pmu/battery", gIODTPlane
)))
282 // If this machine has a battery, publish the fact that the backlight
284 // Notice similar call in IOPMrootDomain::batteryLocationPublished() to
285 // detect batteries on SMU machines.
286 publishFeature("DisplayDims");
287 temp_entry
->release();
290 IOHibernateSystemInit(this);
292 registerService(); // let clients find us
297 IOReturn
IOPMrootDomain::setPMSetting(int type
, OSNumber
*n
)
299 if(_settingController
&& _settingController
->func
) {
301 seconds
= n
->unsigned32BitValue();
302 return (*(_settingController
->func
))(type
, seconds
, _settingController
->refcon
);
304 return kIOReturnNoDevice
;
308 // **********************************************************************************
311 // Receive a setProperty call
312 // The "System Boot" property means the system is completely booted.
313 // **********************************************************************************
314 IOReturn
IOPMrootDomain::setProperties ( OSObject
*props_obj
)
316 IOReturn return_value
= kIOReturnSuccess
;
317 OSDictionary
*dict
= OSDynamicCast(OSDictionary
, props_obj
);
321 const OSSymbol
*boot_complete_string
= OSSymbol::withCString("System Boot Complete");
322 const OSSymbol
*power_button_string
= OSSymbol::withCString("DisablePowerButtonSleep");
323 const OSSymbol
*stall_halt_string
= OSSymbol::withCString("StallSystemAtHalt");
324 const OSSymbol
*auto_wake_string
= OSSymbol::withCString("wake");
325 const OSSymbol
*auto_power_string
= OSSymbol::withCString("poweron");
326 const OSSymbol
*wakeonring_string
= OSSymbol::withCString("WakeOnRing");
327 const OSSymbol
*fileserver_string
= OSSymbol::withCString("AutoRestartOnPowerLoss");
328 const OSSymbol
*wakeonlid_string
= OSSymbol::withCString("WakeOnLid");
329 const OSSymbol
*wakeonac_string
= OSSymbol::withCString("WakeOnACChange");
330 const OSSymbol
*hibernatemode_string
= OSSymbol::withCString(kIOHibernateModeKey
);
331 const OSSymbol
*hibernatefile_string
= OSSymbol::withCString(kIOHibernateFileKey
);
332 const OSSymbol
*hibernatefreeratio_string
= OSSymbol::withCString(kIOHibernateFreeRatioKey
);
333 const OSSymbol
*hibernatefreetime_string
= OSSymbol::withCString(kIOHibernateFreeTimeKey
);
334 const OSSymbol
*timezone_string
= OSSymbol::withCString("TimeZoneOffsetSeconds");
338 return_value
= kIOReturnBadArgument
;
343 && boot_complete_string
344 && dict
->getObject(boot_complete_string
))
346 systemBooting
= false;
350 if( power_button_string
351 && (b
= OSDynamicCast(OSBoolean
, dict
->getObject(power_button_string
))) )
353 setProperty(power_button_string
, b
);
356 if( stall_halt_string
357 && (b
= OSDynamicCast(OSBoolean
, dict
->getObject(stall_halt_string
))) )
359 setProperty(stall_halt_string
, b
);
362 if ( hibernatemode_string
363 && (n
= OSDynamicCast(OSNumber
, dict
->getObject(hibernatemode_string
))))
365 setProperty(hibernatemode_string
, n
);
367 if ( hibernatefreeratio_string
368 && (n
= OSDynamicCast(OSNumber
, dict
->getObject(hibernatefreeratio_string
))))
370 setProperty(hibernatefreeratio_string
, n
);
372 if ( hibernatefreetime_string
373 && (n
= OSDynamicCast(OSNumber
, dict
->getObject(hibernatefreetime_string
))))
375 setProperty(hibernatefreetime_string
, n
);
377 if ( hibernatefile_string
378 && (str
= OSDynamicCast(OSString
, dict
->getObject(hibernatefile_string
))))
380 setProperty(hibernatefile_string
, str
);
383 // Relay AutoWake setting to its controller
385 && (n
= OSDynamicCast(OSNumber
, dict
->getObject(auto_wake_string
))) )
387 return_value
= setPMSetting(kIOPMAutoWakeSetting
, n
);
388 if(kIOReturnSuccess
!= return_value
) goto exit
;
391 // Relay AutoPower setting to its controller
392 if( auto_power_string
393 && (n
= OSDynamicCast(OSNumber
, dict
->getObject(auto_power_string
))) )
395 return_value
= setPMSetting(kIOPMAutoPowerOnSetting
, n
);
396 if(kIOReturnSuccess
!= return_value
) goto exit
;
399 // Relay WakeOnRing setting to its controller
400 if( wakeonring_string
401 && (n
= OSDynamicCast(OSNumber
, dict
->getObject(wakeonring_string
))) )
403 return_value
= setPMSetting(kIOPMWakeOnRingSetting
, n
);
404 if(kIOReturnSuccess
!= return_value
) goto exit
;
407 // Relay FileServer setting to its controller
408 if( fileserver_string
409 && (n
= OSDynamicCast(OSNumber
, dict
->getObject(fileserver_string
))) )
411 return_value
= setPMSetting(kIOPMAutoRestartOnPowerLossSetting
, n
);
412 if(kIOReturnSuccess
!= return_value
) goto exit
;
415 // Relay WakeOnLid setting to its controller
417 && (n
= OSDynamicCast(OSNumber
, dict
->getObject(wakeonlid_string
))) )
419 return_value
= setPMSetting(kIOPMWakeOnLidSetting
, n
);
420 if(kIOReturnSuccess
!= return_value
) goto exit
;
423 // Relay WakeOnACChange setting to its controller
425 && (n
= OSDynamicCast(OSNumber
, dict
->getObject(wakeonac_string
))) )
427 return_value
= setPMSetting(kIOPMWakeOnACChangeSetting
, n
);
428 if(kIOReturnSuccess
!= return_value
) goto exit
;
431 // Relay timezone offset in seconds to SMU
433 && (n
= OSDynamicCast(OSNumber
, dict
->getObject(timezone_string
))) )
435 return_value
= setPMSetting(kIOPMTimeZoneSetting
, n
);
436 if(kIOReturnSuccess
!= return_value
) goto exit
;
441 if(boot_complete_string
) boot_complete_string
->release();
442 if(power_button_string
) power_button_string
->release();
443 if(stall_halt_string
) stall_halt_string
->release();
444 if(auto_wake_string
) auto_wake_string
->release();
445 if(auto_power_string
) auto_power_string
->release();
446 if(wakeonring_string
) wakeonring_string
->release();
447 if(fileserver_string
) fileserver_string
->release();
448 if(wakeonlid_string
) wakeonlid_string
->release();
449 if(wakeonac_string
) wakeonac_string
->release();
450 if(timezone_string
) timezone_string
->release();
455 //*********************************************************************************
458 // Power Managment is informing us that we are the root power domain.
459 // We know we are not the root however, since we have just instantiated a parent
460 // for ourselves and made it the root. We override this method so it will have
462 //*********************************************************************************
463 IOReturn
IOPMrootDomain::youAreRoot ( void )
468 // **********************************************************************************
472 // **********************************************************************************
473 void IOPMrootDomain::command_received ( void * w
, void * x
, void * y
, void * z
)
475 super::command_received(w
,x
,y
,z
);
479 // **********************************************************************************
480 // broadcast_aggressiveness
482 // **********************************************************************************
483 IOReturn
broadcast_aggressiveness ( OSObject
* root
, void * x
, void * y
, void *, void * )
485 ((IOPMrootDomain
*)root
)->broadcast_it((unsigned long)x
,(unsigned long)y
);
490 // **********************************************************************************
493 // We are behind the command gate to broadcast an aggressiveness factor. We let the
494 // superclass do it, but we need to snoop on factors that affect idle sleep.
495 // **********************************************************************************
496 void IOPMrootDomain::broadcast_it (unsigned long type
, unsigned long value
)
498 super::setAggressiveness(type
,value
);
500 // Save user's spin down timer to restore after we replace it for idle sleep
501 if( type
== kPMMinutesToSpinDown
) user_spindown
= value
;
503 // Use longestNonSleepSlider to calculate dimming adjust idle sleep timer
504 longestNonSleepSlider
= pm_vars
->current_aggressiveness_values
[kPMMinutesToDim
];
507 if ( type
== kPMMinutesToSleep
) {
508 if ( (sleepSlider
== 0) && (value
!= 0) ) {
510 // idle sleep is now enabled, maybe sleep now
514 if ( sleepSlider
== 0 ) {
515 // idle sleep is now disabled
517 // make sure we're powered
518 patriarch
->wakeSystem();
521 if ( sleepSlider
> longestNonSleepSlider
) {
522 extraSleepDelay
= sleepSlider
- longestNonSleepSlider
;
530 // **********************************************************************************
533 // **********************************************************************************
534 static void sleepTimerExpired ( thread_call_param_t us
)
536 ((IOPMrootDomain
*)us
)->handleSleepTimerExpiration();
540 static void wakeupClamshellTimerExpired ( thread_call_param_t us
)
542 ((IOPMrootDomain
*)us
)->stopIgnoringClamshellEventsDuringWakeup();
546 // **********************************************************************************
547 // handleSleepTimerExpiration
549 // The time between the sleep idle timeout and the next longest one has elapsed.
550 // It's time to sleep. Start that by removing the clamp that's holding us awake.
551 // **********************************************************************************
552 void IOPMrootDomain::handleSleepTimerExpiration ( void )
554 // accelerate disk spin down if spin down timer is non-zero (zero = never spin down)
555 if(0 != user_spindown
)
556 setQuickSpinDownTimeout();
563 void IOPMrootDomain::stopIgnoringClamshellEventsDuringWakeup(void)
567 // Allow clamshell-induced sleep now
568 ignoringClamshellDuringWakeup
= false;
570 if ((state
= getProperty(kAppleClamshellStateKey
)))
571 publishResource(kAppleClamshellStateKey
, state
);
574 //*********************************************************************************
577 // Some aggressiveness factor has changed. We broadcast it to the hierarchy while on
578 // the Power Mangement workloop thread. This enables objects in the
579 // hierarchy to successfully alter their idle timers, which are all on the
581 //*********************************************************************************
583 static int pmsallsetup
= 0;
585 IOReturn
IOPMrootDomain::setAggressiveness ( unsigned long type
, unsigned long newLevel
)
588 if(pmsExperimental
& 3) kprintf("setAggressiveness: type = %08X, newlevel = %08X\n", type
, newLevel
);
589 if(pmsExperimental
& 1) { /* Is experimental mode enabled? */
590 if(pmsInstalled
&& (type
== kPMSetProcessorSpeed
)) { /* We want to look at all processor speed changes if stepper is installed */
591 if(pmsallsetup
) return kIOReturnSuccess
; /* If already running, just eat this */
592 kprintf("setAggressiveness: starting stepper...\n");
593 pmsallsetup
= 1; /* Remember we did this */
595 pmsStart(); /* Get it all started up... */
596 return kIOReturnSuccess
; /* Leave now... */
601 if ( pm_vars
->PMcommandGate
) {
602 pm_vars
->PMcommandGate
->runAction(broadcast_aggressiveness
,(void *)type
,(void *)newLevel
);
605 return kIOReturnSuccess
;
609 // **********************************************************************************
612 // **********************************************************************************
613 IOReturn
IOPMrootDomain::sleepSystem ( void )
615 //kprintf("sleep demand received\n");
616 if ( !systemBooting
&& allowSleep
&& sleepIsSupported
) {
617 patriarch
->sleepSystem();
619 return kIOReturnSuccess
;
621 if ( !systemBooting
&& allowSleep
&& !sleepIsSupported
) {
622 patriarch
->dozeSystem();
623 return kIOReturnSuccess
;
625 return kIOReturnSuccess
;
629 // **********************************************************************************
632 // **********************************************************************************
633 IOReturn
IOPMrootDomain::shutdownSystem ( void )
635 //patriarch->shutDownSystem();
636 return kIOReturnUnsupported
;
640 // **********************************************************************************
643 // **********************************************************************************
644 IOReturn
IOPMrootDomain::restartSystem ( void )
646 //patriarch->restartSystem();
647 return kIOReturnUnsupported
;
651 // **********************************************************************************
654 // This overrides powerChangeDone in IOService.
656 // Finder sleep and idle sleep move us from the ON state to the SLEEP_STATE.
658 // If we just finished going to the SLEEP_STATE, and the platform is capable of true sleep,
659 // sleep the kernel. Otherwise switch up to the DOZE_STATE which will keep almost
660 // everything as off as it can get.
662 // **********************************************************************************
663 void IOPMrootDomain::powerChangeDone ( unsigned long previousState
)
665 OSNumber
* propertyPtr
;
666 unsigned short theProperty
;
667 AbsoluteTime deadline
;
669 switch ( pm_vars
->myCurrentState
) {
671 if ( canSleep
&& sleepIsSupported
)
673 // re-enable this timer for next sleep
674 idleSleepPending
= false;
676 IOLog("System %sSleep\n", gIOHibernateState
? "Safe" : "");
678 IOHibernateSystemHasSlept();
680 pm_vars
->thePlatform
->sleepKernel();
682 // The CPU(s) are off at this point. When they're awakened by CPU interrupt,
683 // code will resume execution here.
685 // Now we're waking...
686 IOHibernateSystemWake();
688 // stay awake for at least 30 seconds
689 clock_interval_to_deadline(30, kSecondScale
, &deadline
);
690 thread_call_enter_delayed(extraSleepTimer
, deadline
);
691 // this gets turned off when we sleep again
692 idleSleepPending
= true;
694 // Ignore closed clamshell during wakeup and for a few seconds
695 // after wakeup is complete
696 ignoringClamshellDuringWakeup
= true;
698 // sleep transition complete
699 gSleepOrShutdownPending
= 0;
701 // trip the reset of the calendar clock
702 clock_wakeup_calendar();
705 patriarch
->wakeSystem();
707 // early stage wake notification
708 tellClients(kIOMessageSystemWillPowerOn
);
710 // tell the tree we're waking
711 IOLog("System %sWake\n", gIOHibernateState
? "SafeSleep " : "");
714 // Allow drivers to request extra processing time before clamshell
715 // sleep if kIOREMSleepEnabledKey is present.
716 // Ignore clamshell events for at least 5 seconds
717 if(getProperty(kIOREMSleepEnabledKey
)) {
718 // clamshellWakeupIgnore callout clears ignoreClamshellDuringWakeup bit
719 clock_interval_to_deadline(5, kSecondScale
, &deadline
);
720 if(clamshellWakeupIgnore
) thread_call_enter_delayed(clamshellWakeupIgnore
, deadline
);
721 } else ignoringClamshellDuringWakeup
= false;
723 // Find out what woke us
724 propertyPtr
= OSDynamicCast(OSNumber
,getProperty("WakeEvent"));
726 theProperty
= propertyPtr
->unsigned16BitValue();
727 IOLog("Wake event %04x\n",theProperty
);
728 if ( (theProperty
& 0x0008) || //lid
729 (theProperty
& 0x0800) || // front panel button
730 (theProperty
& 0x0020) || // external keyboard
731 (theProperty
& 0x0001) ) { // internal keyboard
732 // We've identified the wakeup event as UI driven
736 // Since we can't identify the wakeup event, treat it as UI activity
740 // Wake for thirty seconds
741 changePowerStateToPriv(ON_STATE
);
742 powerOverrideOffPriv();
744 // allow us to step up a power state
745 patriarch
->sleepToDoze();
747 changePowerStateToPriv(DOZE_STATE
);
752 if ( previousState
!= DOZE_STATE
)
754 IOLog("System Doze\n");
756 // re-enable this timer for next sleep
757 idleSleepPending
= false;
758 gSleepOrShutdownPending
= 0;
762 IOLog("System Restart\n");
763 PEHaltRestart(kPERestartCPU
);
767 IOLog("System Halt\n");
768 PEHaltRestart(kPEHaltCPU
);
774 // **********************************************************************************
777 // The Display Wrangler calls here when it switches to its highest state. If the
778 // system is currently dozing, allow it to wake by making sure the parent is
780 // **********************************************************************************
781 void IOPMrootDomain::wakeFromDoze( void )
783 if ( pm_vars
->myCurrentState
== DOZE_STATE
)
785 // reset this till next attempt
787 powerOverrideOffPriv();
789 // early wake notification
790 tellClients(kIOMessageSystemWillPowerOn
);
792 // allow us to wake if children so desire
793 patriarch
->wakeSystem();
798 // **********************************************************************************
801 // Adds a new feature to the supported features dictionary
804 // **********************************************************************************
805 void IOPMrootDomain::publishFeature( const char * feature
)
807 if(featuresDictLock
) IOLockLock(featuresDictLock
);
808 OSDictionary
*features
=
809 (OSDictionary
*) getProperty(kRootDomainSupportedFeatures
);
811 if ( features
&& OSDynamicCast(OSDictionary
, features
))
812 features
= OSDictionary::withDictionary(features
);
814 features
= OSDictionary::withCapacity(1);
816 features
->setObject(feature
, kOSBooleanTrue
);
817 setProperty(kRootDomainSupportedFeatures
, features
);
819 if(featuresDictLock
) IOLockUnlock(featuresDictLock
);
823 void IOPMrootDomain::unIdleDevice( IOService
*theDevice
, unsigned long theState
)
825 if(pmPowerStateQueue
)
826 pmPowerStateQueue
->unIdleOccurred(theDevice
, theState
);
829 void IOPMrootDomain::announcePowerSourceChange( void )
831 IORegistryEntry
*_batteryRegEntry
= (IORegistryEntry
*) getProperty("BatteryEntry");
833 // (if possible) re-publish power source state under IOPMrootDomain
834 // (only done if the battery controller publishes an IOResource defining battery location)
838 batt_info
= (OSArray
*) _batteryRegEntry
->getProperty(kIOBatteryInfoKey
);
840 setProperty(kIOBatteryInfoKey
, batt_info
);
843 messageClients(kIOPMMessageBatteryStatusHasChanged
);
846 IOReturn
IOPMrootDomain::registerPMSettingController
847 (IOPMSettingControllerCallback func
, void *info
)
849 if(_settingController
) return kIOReturnExclusiveAccess
;
851 _settingController
= (PMSettingCtrl
*)IOMalloc(sizeof(PMSettingCtrl
));
852 if(!_settingController
) return kIOReturnNoMemory
;
854 _settingController
->func
= func
;
855 _settingController
->refcon
= info
;
856 return kIOReturnSuccess
;
860 //*********************************************************************************
861 // receivePowerNotification
863 // The power controller is notifying us of a hardware-related power management
864 // event that we must handle. This is a result of an 'environment' interrupt from
865 // the power mgt micro.
866 //*********************************************************************************
868 IOReturn
IOPMrootDomain::receivePowerNotification (UInt32 msg
)
870 if (msg
& kIOPMOverTemp
)
872 IOLog("Power Management received emergency overtemp signal. Going to sleep.");
873 (void) sleepSystem ();
875 if (msg
& kIOPMSetDesktopMode
)
877 desktopMode
= (0 != (msg
& kIOPMSetValue
));
878 msg
&= ~(kIOPMSetDesktopMode
| kIOPMSetValue
);
880 if (msg
& kIOPMSetACAdaptorConnected
)
882 acAdaptorConnect
= (0 != (msg
& kIOPMSetValue
));
883 msg
&= ~(kIOPMSetACAdaptorConnected
| kIOPMSetValue
);
885 if (msg
& kIOPMEnableClamshell
)
887 ignoringClamshell
= false;
889 if (msg
& kIOPMDisableClamshell
)
891 ignoringClamshell
= true;
894 if (msg
& kIOPMProcessorSpeedChange
)
896 IOService
*pmu
= waitForService(serviceMatching("ApplePMU"));
897 pmu
->callPlatformFunction("prepareForSleep", false, 0, 0, 0, 0);
898 pm_vars
->thePlatform
->sleepKernel();
899 pmu
->callPlatformFunction("recoverFromSleep", false, 0, 0, 0, 0);
902 if (msg
& kIOPMSleepNow
)
904 (void) sleepSystem ();
907 if (msg
& kIOPMPowerEmergency
)
909 (void) sleepSystem ();
912 if (msg
& kIOPMClamshellClosed
)
914 if ( !ignoringClamshell
&& !ignoringClamshellDuringWakeup
915 && (!desktopMode
|| !acAdaptorConnect
) )
918 (void) sleepSystem ();
922 if (msg
& kIOPMPowerButton
)
924 // toggle state of sleep/wake
926 if ( pm_vars
->myCurrentState
== DOZE_STATE
)
928 // yes, tell the tree we're waking
930 // wake the Display Wrangler
934 // Check that power button sleep is enabled
935 if(kOSBooleanTrue
!= getProperty(OSString::withCString("DisablePowerButtonSleep")))
940 // if the case has been closed, we allow
941 // the machine to be put to sleep or to idle sleep
943 if ( (msg
& kIOPMAllowSleep
) && !allowSleep
)
949 // if the case has been opened, we disallow sleep/doze
951 if (msg
& kIOPMPreventSleep
) {
954 if ( pm_vars
->myCurrentState
== DOZE_STATE
) {
955 // yes, tell the tree we're waking
958 // wake the Display Wrangler
962 // make sure we have power to clamp
963 patriarch
->wakeSystem();
971 //*********************************************************************************
974 //*********************************************************************************
976 void IOPMrootDomain::setSleepSupported( IOOptionBits flags
)
978 if ( flags
& kPCICantSleep
)
982 platformSleepSupport
= flags
;
987 //*********************************************************************************
988 // requestPowerDomainState
990 // The root domain intercepts this call to the superclass.
992 // If the clamp bit is not set in the desire, then the child doesn't need the power
993 // state it's requesting; it just wants it. The root ignores desires but not needs.
994 // If the clamp bit is not set, the root takes it that the child can tolerate no
995 // power and interprets the request accordingly. If all children can thus tolerate
996 // no power, we are on our way to idle sleep.
997 //*********************************************************************************
999 IOReturn
IOPMrootDomain::requestPowerDomainState ( IOPMPowerFlags desiredState
, IOPowerConnection
* whichChild
, unsigned long specification
)
1003 IOPowerConnection
*connection
;
1004 unsigned long powerRequestFlag
= 0;
1005 IOPMPowerFlags editedDesire
= desiredState
;
1007 // if they don't really need it, they don't get it
1008 if ( !(desiredState
& kIOPMPreventIdleSleep
) ) {
1013 IOLockLock(pm_vars
->childLock
);
1015 // recompute sleepIsSupported and see if all children are asleep
1016 iter
= getChildIterator(gIOPowerPlane
);
1017 sleepIsSupported
= true;
1020 while ( (next
= iter
->getNextObject()) )
1022 if ( (connection
= OSDynamicCast(IOPowerConnection
,next
)) )
1024 if ( connection
== whichChild
)
1026 powerRequestFlag
+= editedDesire
;
1027 if ( desiredState
& kIOPMPreventSystemSleep
)
1029 sleepIsSupported
= false;
1032 powerRequestFlag
+= connection
->getDesiredDomainState();
1033 if ( connection
->getPreventSystemSleepFlag() )
1035 sleepIsSupported
= false;
1043 if ( (extraSleepDelay
== 0) && (powerRequestFlag
== 0) )
1048 // this may put the system to sleep
1051 IOLockUnlock(pm_vars
->childLock
);
1053 editedDesire
|= desiredState
& kIOPMPreventSystemSleep
;
1055 return super::requestPowerDomainState(editedDesire
,whichChild
,specification
);
1059 //*********************************************************************************
1060 // getSleepSupported
1062 //*********************************************************************************
1064 IOOptionBits
IOPMrootDomain::getSleepSupported( void )
1066 return( platformSleepSupport
);
1070 //*********************************************************************************
1073 // We override the superclass implementation so we can send a different message
1074 // type to the client or application being notified.
1075 //*********************************************************************************
1077 bool IOPMrootDomain::tellChangeDown ( unsigned long stateNum
)
1079 switch ( stateNum
) {
1082 return super::tellClientsWithResponse(kIOMessageSystemWillSleep
);
1084 return super::tellClientsWithResponse(kIOMessageSystemWillRestart
);
1086 return super::tellClientsWithResponse(kIOMessageSystemWillPowerOff
);
1088 // this shouldn't execute
1089 return super::tellChangeDown(stateNum
);
1093 //*********************************************************************************
1096 // We override the superclass implementation so we can send a different message
1097 // type to the client or application being notified.
1099 // This must be idle sleep since we don't ask apps during any other power change.
1100 //*********************************************************************************
1102 bool IOPMrootDomain::askChangeDown ( unsigned long )
1104 return super::tellClientsWithResponse(kIOMessageCanSystemSleep
);
1108 //*********************************************************************************
1111 // Notify registered applications and kernel clients that we are not
1114 // We override the superclass implementation so we can send a different message
1115 // type to the client or application being notified.
1117 // This must be a vetoed idle sleep, since no other power change can be vetoed.
1118 //*********************************************************************************
1120 void IOPMrootDomain::tellNoChangeDown ( unsigned long )
1122 return tellClients(kIOMessageSystemWillNotSleep
);
1126 //*********************************************************************************
1129 // Notify registered applications and kernel clients that we are raising power.
1131 // We override the superclass implementation so we can send a different message
1132 // type to the client or application being notified.
1133 //*********************************************************************************
1135 void IOPMrootDomain::tellChangeUp ( unsigned long stateNum
)
1137 if ( stateNum
== ON_STATE
)
1139 IOHibernateSystemPostWake();
1140 return tellClients(kIOMessageSystemHasPoweredOn
);
1144 //*********************************************************************************
1147 //*********************************************************************************
1149 void IOPMrootDomain::reportUserInput ( void )
1155 iter
= getMatchingServices(serviceMatching("IODisplayWrangler"));
1158 wrangler
= (IOService
*) iter
->getNextObject();
1164 wrangler
->activityTickle(0,0);
1167 //*********************************************************************************
1168 // setQuickSpinDownTimeout
1170 //*********************************************************************************
1172 void IOPMrootDomain::setQuickSpinDownTimeout ( void )
1174 super::setAggressiveness((unsigned long)kPMMinutesToSpinDown
,(unsigned long)1);
1177 //*********************************************************************************
1178 // restoreUserSpinDownTimeout
1180 //*********************************************************************************
1182 void IOPMrootDomain::restoreUserSpinDownTimeout ( void )
1184 super::setAggressiveness((unsigned long)kPMMinutesToSpinDown
,(unsigned long)user_spindown
);
1187 //*********************************************************************************
1188 // changePowerStateTo & changePowerStateToPriv
1190 // Override of these methods for logging purposes.
1191 //*********************************************************************************
1193 IOReturn
IOPMrootDomain::changePowerStateTo ( unsigned long ordinal
)
1195 return super::changePowerStateTo(ordinal
);
1198 IOReturn
IOPMrootDomain::changePowerStateToPriv ( unsigned long ordinal
)
1200 return super::changePowerStateToPriv(ordinal
);
1204 //*********************************************************************************
1205 // sysPowerDownHandler
1207 // Receives a notification when the RootDomain changes state.
1209 // Allows us to take action on system sleep, power down, and restart after
1210 // applications have received their power change notifications and replied,
1211 // but before drivers have powered down. We perform a vfs sync on power down.
1212 //*********************************************************************************
1214 IOReturn
IOPMrootDomain::sysPowerDownHandler( void * target
, void * refCon
,
1215 UInt32 messageType
, IOService
* service
,
1216 void * messageArgument
, vm_size_t argSize
)
1219 IOPowerStateChangeNotification
*params
= (IOPowerStateChangeNotification
*) messageArgument
;
1220 IOPMrootDomain
*rootDomain
= OSDynamicCast(IOPMrootDomain
, service
);
1223 return kIOReturnUnsupported
;
1225 switch (messageType
) {
1226 case kIOMessageSystemWillSleep
:
1227 rootDomain
->powerOverrideOnPriv(); // start ignoring children's requests
1228 // (fall through to other cases)
1230 // Interested applications have been notified of an impending power
1231 // change and have acked (when applicable).
1232 // This is our chance to save whatever state we can before powering
1234 // We call sync_internal defined in xnu/bsd/vfs/vfs_syscalls.c,
1237 // We will ack within 20 seconds
1238 params
->returnValue
= 20 * 1000 * 1000;
1239 if (gIOHibernateState
)
1240 params
->returnValue
+= gIOHibernateFreeTime
* 1000; //add in time we could spend freeing pages
1242 if ( ! OSCompareAndSwap( 0, 1, &gSleepOrShutdownPending
) )
1244 // Purposely delay the ack and hope that shutdown occurs quickly.
1245 // Another option is not to schedule the thread and wait for
1247 AbsoluteTime deadline
;
1248 clock_interval_to_deadline( 30, kSecondScale
, &deadline
);
1249 thread_call_enter1_delayed( rootDomain
->diskSyncCalloutEntry
,
1250 (thread_call_param_t
)params
->powerRef
,
1254 thread_call_enter1(rootDomain
->diskSyncCalloutEntry
, (thread_call_param_t
)params
->powerRef
);
1255 ret
= kIOReturnSuccess
;
1258 case kIOMessageSystemWillPowerOff
:
1259 case kIOMessageSystemWillRestart
:
1260 ret
= kIOReturnUnsupported
;
1264 ret
= kIOReturnUnsupported
;
1270 //*********************************************************************************
1271 // displayWranglerNotification
1273 // Receives a notification when the IODisplayWrangler changes state.
1275 // Allows us to take action on display dim/undim.
1277 // When the display goes dim we:
1278 // - Start the idle sleep timer
1279 // - set the quick spin down timeout
1281 // On wake from display dim:
1282 // - Cancel the idle sleep timer
1283 // - restore the user's chosen spindown timer from the "quick" spin down value
1284 //*********************************************************************************
1286 IOReturn
IOPMrootDomain::displayWranglerNotification( void * target
, void * refCon
,
1287 UInt32 messageType
, IOService
* service
,
1288 void * messageArgument
, vm_size_t argSize
)
1290 IOPMrootDomain
* rootDomain
= OSDynamicCast(IOPMrootDomain
, (IOService
*)target
);
1291 AbsoluteTime deadline
;
1292 static bool deviceAlreadyPoweredOff
= false;
1295 return kIOReturnUnsupported
;
1297 switch (messageType
) {
1298 case kIOMessageDeviceWillPowerOff
:
1299 // The IODisplayWrangler has powered off either because of idle display sleep
1300 // or force system sleep.
1302 // The display wrangler will send the DeviceWillPowerOff message 4 times until
1303 // it gets into its lowest state. We only want to act on the first of those 4.
1304 if( deviceAlreadyPoweredOff
) return kIOReturnUnsupported
;
1306 deviceAlreadyPoweredOff
= true;
1308 if( rootDomain
->extraSleepDelay
)
1310 // start the extra sleep timer
1311 clock_interval_to_deadline(rootDomain
->extraSleepDelay
*60, kSecondScale
, &deadline
);
1312 thread_call_enter_delayed(rootDomain
->extraSleepTimer
, deadline
);
1313 rootDomain
->idleSleepPending
= true;
1315 // accelerate disk spin down if spin down timer is non-zero (zero = never spin down)
1316 // and if system sleep is non-Never
1317 if( (0 != rootDomain
->user_spindown
) && (0 != rootDomain
->sleepSlider
) )
1318 rootDomain
->setQuickSpinDownTimeout();
1323 case kIOMessageDeviceHasPoweredOn
:
1325 // The display has powered on either because of UI activity or wake from sleep/doze
1326 deviceAlreadyPoweredOff
= false;
1327 rootDomain
->adjustPowerState();
1330 // cancel any pending idle sleep
1331 if(rootDomain
->idleSleepPending
)
1333 thread_call_cancel(rootDomain
->extraSleepTimer
);
1334 rootDomain
->idleSleepPending
= false;
1337 // Change the spindown value back to the user's selection from our accelerated setting
1338 if(0 != rootDomain
->user_spindown
)
1339 rootDomain
->restoreUserSpinDownTimeout();
1341 // Put on the policy maker's on clamp.
1348 return kIOReturnUnsupported
;
1351 //*********************************************************************************
1352 // displayWranglerPublished
1354 // Receives a notification when the IODisplayWrangler is published.
1355 // When it's published we install a power state change handler.
1357 //*********************************************************************************
1359 bool IOPMrootDomain::displayWranglerPublished( void * target
, void * refCon
,
1360 IOService
* newService
)
1362 IOPMrootDomain
* rootDomain
= OSDynamicCast(IOPMrootDomain
, (IOService
*)target
);
1367 rootDomain
->wrangler
= newService
;
1369 // we found the display wrangler, now install a handler
1370 if( !rootDomain
->wrangler
->registerInterest( gIOGeneralInterest
, &displayWranglerNotification
, target
, 0) ) {
1371 IOLog("IOPMrootDomain::displayWranglerPublished registerInterest failed\n");
1378 //*********************************************************************************
1379 // batteryLocationPublished
1381 // Notification on AppleSMU publishing location of battery data
1383 //*********************************************************************************
1385 bool IOPMrootDomain::batteryLocationPublished( void * target
, void * root_domain
,
1386 IOService
* resourceService
)
1388 IORegistryEntry
*battery_location
;
1390 battery_location
= (IORegistryEntry
*) resourceService
->getProperty("battery");
1391 if (!battery_location
|| !OSDynamicCast(IORegistryEntry
, battery_location
))
1394 ((IOPMrootDomain
*)root_domain
)->setProperty("BatteryEntry", battery_location
);
1397 // All laptops have dimmable LCD displays
1398 // All laptops have batteries
1399 // So if this machine has a battery, publish the fact that the backlight
1400 // supports dimming.
1401 ((IOPMrootDomain
*)root_domain
)->publishFeature("DisplayDims");
1403 ((IOPMrootDomain
*)root_domain
)->announcePowerSourceChange();
1409 //*********************************************************************************
1412 // Some condition that affects our wake/sleep/doze decision has changed.
1414 // If the sleep slider is in the off position, we cannot sleep or doze.
1415 // If the enclosure is open, we cannot sleep or doze.
1416 // If the system is still booting, we cannot sleep or doze.
1418 // In those circumstances, we prevent sleep and doze by holding power on with
1419 // changePowerStateToPriv(ON).
1421 // If the above conditions do not exist, and also the sleep timer has expired, we
1422 // allow sleep or doze to occur with either changePowerStateToPriv(SLEEP) or
1423 // changePowerStateToPriv(DOZE) depending on whether or not we already know the
1424 // platform cannot sleep.
1426 // In this case, sleep or doze will either occur immediately or at the next time
1427 // that no children are holding the system out of idle sleep via the
1428 // kIOPMPreventIdleSleep flag in their power state arrays.
1429 //*********************************************************************************
1431 void IOPMrootDomain::adjustPowerState( void )
1433 if ( (sleepSlider
== 0) ||
1436 changePowerStateToPriv(ON_STATE
);
1441 if ( sleepIsSupported
)
1443 changePowerStateToPriv(SLEEP_STATE
);
1445 changePowerStateToPriv(DOZE_STATE
);
1452 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1455 #define super IOService
1457 OSDefineMetaClassAndStructors(IORootParent
, IOService
)
1459 // This array exactly parallels the state array for the root domain.
1460 // Power state changes initiated by a device can be vetoed by a client of the device, and
1461 // power state changes initiated by the parent of a device cannot be vetoed by a client of the device,
1462 // so when the root domain wants a power state change that cannot be vetoed (e.g. demand sleep), it asks
1463 // its parent to make the change. That is the reason for this complexity.
1465 static IOPMPowerState patriarchPowerStates
[number_of_power_states
] = {
1466 {1,0,0,0,0,0,0,0,0,0,0,0}, // off
1467 {1,0,RESTART_POWER
,0,0,0,0,0,0,0,0,0}, // reset
1468 {1,0,SLEEP_POWER
,0,0,0,0,0,0,0,0,0}, // sleep
1469 {1,0,DOZE_POWER
,0,0,0,0,0,0,0,0,0}, // doze
1470 {1,0,ON_POWER
,0,0,0,0,0,0,0,0,0} // running
1473 bool IORootParent::start ( IOService
* nub
)
1475 mostRecentChange
= ON_STATE
;
1478 registerPowerDriver(this,patriarchPowerStates
,number_of_power_states
);
1479 powerOverrideOnPriv();
1484 void IORootParent::shutDownSystem ( void )
1486 mostRecentChange
= OFF_STATE
;
1487 changePowerStateToPriv(OFF_STATE
);
1491 void IORootParent::restartSystem ( void )
1493 mostRecentChange
= RESTART_STATE
;
1494 changePowerStateToPriv(RESTART_STATE
);
1498 void IORootParent::sleepSystem ( void )
1500 mostRecentChange
= SLEEP_STATE
;
1501 changePowerStateToPriv(SLEEP_STATE
);
1505 void IORootParent::dozeSystem ( void )
1507 mostRecentChange
= DOZE_STATE
;
1508 changePowerStateToPriv(DOZE_STATE
);
1511 // Called in demand sleep when sleep discovered to be impossible after actually attaining that state.
1512 // This brings the parent to doze, which allows the root to step up from sleep to doze.
1514 // In idle sleep, do nothing because the parent is still on and the root can freely change state.
1516 void IORootParent::sleepToDoze ( void )
1518 if ( mostRecentChange
== SLEEP_STATE
) {
1519 changePowerStateToPriv(DOZE_STATE
);
1524 void IORootParent::wakeSystem ( void )
1526 mostRecentChange
= ON_STATE
;
1527 changePowerStateToPriv(ON_STATE
);