xref: /xnu-11215.41.3/iokit/Kernel/IOPMrootDomain.cpp (revision 33de042d024d46de5ff4e89f2471de6608e37fa4)
1 /*
2  * Copyright (c) 1998-2021 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
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. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 #define IOKIT_ENABLE_SHARED_PTR
30 
31 #include <libkern/c++/OSAllocation.h>
32 #include <libkern/c++/OSKext.h>
33 #include <libkern/c++/OSMetaClass.h>
34 #include <libkern/OSAtomic.h>
35 #include <libkern/OSDebug.h>
36 #include <IOKit/IOWorkLoop.h>
37 #include <IOKit/IOCommandGate.h>
38 #include <IOKit/IOTimerEventSource.h>
39 #include <IOKit/IOPlatformExpert.h>
40 #include <IOKit/IOCPU.h>
41 #include <IOKit/IOPlatformActions.h>
42 #include <IOKit/IOKitDebug.h>
43 #include <IOKit/IOTimeStamp.h>
44 #include <IOKit/pwr_mgt/IOPMlog.h>
45 #include <IOKit/pwr_mgt/RootDomain.h>
46 #include <IOKit/pwr_mgt/IOPMPrivate.h>
47 #include <IOKit/IODeviceTreeSupport.h>
48 #include <IOKit/IOMessage.h>
49 #include <IOKit/IOReturn.h>
50 #include <IOKit/IONVRAM.h>
51 #include "RootDomainUserClient.h"
52 #include "IOKit/pwr_mgt/IOPowerConnection.h"
53 #include "IOPMPowerStateQueue.h"
54 #include <IOKit/IOCatalogue.h>
55 #include <IOKit/IOReportMacros.h>
56 #include <IOKit/IOLib.h>
57 #include <IOKit/IOKitKeys.h>
58 #include <IOKit/IOUserServer.h>
59 #include "IOKitKernelInternal.h"
60 #if HIBERNATION
61 #include <IOKit/IOHibernatePrivate.h>
62 #endif /* HIBERNATION */
63 #include <machine/machine_routines.h>
64 #include <console/video_console.h>
65 #include <sys/syslog.h>
66 #include <sys/sysctl.h>
67 #include <sys/vnode.h>
68 #include <sys/vnode_internal.h>
69 #include <sys/fcntl.h>
70 #include <os/log.h>
71 #include <pexpert/protos.h>
72 #include <AssertMacros.h>
73 
74 #include <sys/time.h>
75 #include "IOServicePrivate.h"   // _IOServiceInterestNotifier
76 #include "IOServicePMPrivate.h"
77 
78 #include <libkern/zlib.h>
79 #include <os/cpp_util.h>
80 #include <os/atomic_private.h>
81 #include <libkern/c++/OSBoundedArrayRef.h>
82 #include <libkern/coreanalytics/coreanalytics.h>
83 
84 #if DEVELOPMENT || DEBUG
85 #include <os/system_event_log.h>
86 #endif /* DEVELOPMENT || DEBUG */
87 
88 __BEGIN_DECLS
89 #include <mach/shared_region.h>
90 #include <kern/clock.h>
91 #include <vm/vm_pageout_xnu.h>
92 __END_DECLS
93 
94 #if defined(__i386__) || defined(__x86_64__)
95 __BEGIN_DECLS
96 #include "IOPMrootDomainInternal.h"
97 const char *processor_to_datastring(const char *prefix, processor_t target_processor);
98 __END_DECLS
99 #endif
100 
101 #define kIOPMrootDomainClass    "IOPMrootDomain"
102 #define LOG_PREFIX              "PMRD: "
103 
104 
105 #define MSG(x...) \
106     do { kprintf(LOG_PREFIX x); IOLog(x); } while (false)
107 
108 #define LOG(x...)    \
109     do { kprintf(LOG_PREFIX x); } while (false)
110 
111 #if DEVELOPMENT || DEBUG
112 #define DEBUG_LOG(x...) do { \
113     if (kIOLogPMRootDomain & gIOKitDebug) \
114     kprintf(LOG_PREFIX x); \
115     os_log_debug(OS_LOG_DEFAULT, LOG_PREFIX x); \
116 } while (false)
117 #else
118 #define DEBUG_LOG(x...)
119 #endif
120 
121 #define DLOG(x...)  do { \
122     if (kIOLogPMRootDomain & gIOKitDebug) \
123 	IOLog(LOG_PREFIX x); \
124     else \
125 	os_log(OS_LOG_DEFAULT, LOG_PREFIX x); \
126 } while (false)
127 
128 #define DMSG(x...)  do { \
129     if (kIOLogPMRootDomain & gIOKitDebug) { \
130 	kprintf(LOG_PREFIX x); \
131     } \
132 } while (false)
133 
134 
135 #define _LOG(x...)
136 
137 #define CHECK_THREAD_CONTEXT
138 #ifdef  CHECK_THREAD_CONTEXT
139 static IOWorkLoop * gIOPMWorkLoop = NULL;
140 #define ASSERT_GATED()                                      \
141 do {                                                        \
142     if (gIOPMWorkLoop && gIOPMWorkLoop->inGate() != true) { \
143 	panic("RootDomain: not inside PM gate");            \
144     }                                                       \
145 } while(false)
146 #else
147 #define ASSERT_GATED()
148 #endif /* CHECK_THREAD_CONTEXT */
149 
150 #define CAP_LOSS(c)  \
151 	(((_pendingCapability & (c)) == 0) && \
152 	 ((_currentCapability & (c)) != 0))
153 
154 #define CAP_GAIN(c)  \
155 	(((_currentCapability & (c)) == 0) && \
156 	 ((_pendingCapability & (c)) != 0))
157 
158 #define CAP_CHANGE(c)    \
159 	(((_currentCapability ^ _pendingCapability) & (c)) != 0)
160 
161 #define CAP_CURRENT(c)  \
162 	((_currentCapability & (c)) != 0)
163 
164 #define CAP_HIGHEST(c)  \
165 	((_highestCapability & (c)) != 0)
166 
167 #define CAP_PENDING(c)  \
168 	((_pendingCapability & (c)) != 0)
169 
170 // rdar://problem/9157444
171 #if defined(__i386__) || defined(__x86_64__)
172 #define DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY   20
173 #endif
174 
175 // Event types for IOPMPowerStateQueue::submitPowerEvent()
176 enum {
177 	kPowerEventFeatureChanged = 1,             // 1
178 	kPowerEventReceivedPowerNotification,      // 2
179 	kPowerEventSystemBootCompleted,            // 3
180 	kPowerEventSystemShutdown,                 // 4
181 	kPowerEventUserDisabledSleep,              // 5
182 	kPowerEventRegisterSystemCapabilityClient, // 6
183 	kPowerEventRegisterKernelCapabilityClient, // 7
184 	kPowerEventPolicyStimulus,                 // 8
185 	kPowerEventAssertionCreate,                // 9
186 	kPowerEventAssertionRelease,               // 10
187 	kPowerEventAssertionSetLevel,              // 11
188 	kPowerEventQueueSleepWakeUUID,             // 12
189 	kPowerEventPublishSleepWakeUUID,           // 13
190 	kPowerEventSetDisplayPowerOn,              // 14
191 	kPowerEventPublishWakeType,                // 15
192 	kPowerEventAOTEvaluate                     // 16
193 };
194 
195 // For evaluatePolicy()
196 // List of stimuli that affects the root domain policy.
197 enum {
198 	kStimulusDisplayWranglerSleep,      // 0
199 	kStimulusDisplayWranglerWake,       // 1
200 	kStimulusAggressivenessChanged,     // 2
201 	kStimulusDemandSystemSleep,         // 3
202 	kStimulusAllowSystemSleepChanged,   // 4
203 	kStimulusDarkWakeActivityTickle,    // 5
204 	kStimulusDarkWakeEntry,             // 6
205 	kStimulusDarkWakeReentry,           // 7
206 	kStimulusDarkWakeEvaluate,          // 8
207 	kStimulusNoIdleSleepPreventers,     // 9
208 	kStimulusEnterUserActiveState,      // 10
209 	kStimulusLeaveUserActiveState       // 11
210 };
211 
212 // Internal power state change reasons
213 // Must be less than kIOPMSleepReasonClamshell=101
214 enum {
215 	kCPSReasonNone = 0,                 // 0
216 	kCPSReasonInit,                     // 1
217 	kCPSReasonWake,                     // 2
218 	kCPSReasonIdleSleepPrevent,         // 3
219 	kCPSReasonIdleSleepAllow,           // 4
220 	kCPSReasonPowerOverride,            // 5
221 	kCPSReasonPowerDownCancel,          // 6
222 	kCPSReasonAOTExit,                  // 7
223 	kCPSReasonAdjustPowerState,         // 8
224 	kCPSReasonDarkWakeCannotSleep,      // 9
225 	kCPSReasonIdleSleepEnabled,         // 10
226 	kCPSReasonEvaluatePolicy,           // 11
227 	kCPSReasonSustainFullWake,          // 12
228 	kCPSReasonPMInternals = (kIOPMSleepReasonClamshell - 1)
229 };
230 
231 extern "C" {
232 IOReturn OSKextSystemSleepOrWake( UInt32 );
233 }
234 extern "C" ppnum_t      pmap_find_phys(pmap_t pmap, addr64_t va);
235 extern "C" addr64_t     kvtophys(vm_offset_t va);
236 extern "C" boolean_t    kdp_has_polled_corefile();
237 
238 static void idleSleepTimerExpired( thread_call_param_t, thread_call_param_t );
239 static void notifySystemShutdown( IOService * root, uint32_t messageType );
240 static void handleAggressivesFunction( thread_call_param_t, thread_call_param_t );
241 static void pmEventTimeStamp(uint64_t *recordTS);
242 static void powerButtonUpCallout( thread_call_param_t, thread_call_param_t );
243 static void powerButtonDownCallout( thread_call_param_t, thread_call_param_t );
244 static OSPtr<const OSSymbol> copyKextIdentifierWithAddress(vm_address_t address);
245 
246 static int  IOPMConvertSecondsToCalendar(clock_sec_t secs, IOPMCalendarStruct * dt);
247 static clock_sec_t IOPMConvertCalendarToSeconds(const IOPMCalendarStruct * dt);
248 #define YMDTF       "%04d/%02d/%d %02d:%02d:%02d"
249 #define YMDT(cal)   ((int)(cal)->year), (cal)->month, (cal)->day, (cal)->hour, (cal)->minute, (cal)->second
250 
251 // "IOPMSetSleepSupported"  callPlatformFunction name
252 static OSSharedPtr<const OSSymbol>         sleepSupportedPEFunction;
253 static OSSharedPtr<const OSSymbol>         sleepMessagePEFunction;
254 static OSSharedPtr<const OSSymbol>         gIOPMWakeTypeUserKey;
255 
256 static OSSharedPtr<const OSSymbol>         gIOPMPSExternalConnectedKey;
257 static OSSharedPtr<const OSSymbol>         gIOPMPSExternalChargeCapableKey;
258 static OSSharedPtr<const OSSymbol>         gIOPMPSBatteryInstalledKey;
259 static OSSharedPtr<const OSSymbol>         gIOPMPSIsChargingKey;
260 static OSSharedPtr<const OSSymbol>         gIOPMPSAtWarnLevelKey;
261 static OSSharedPtr<const OSSymbol>         gIOPMPSAtCriticalLevelKey;
262 static OSSharedPtr<const OSSymbol>         gIOPMPSCurrentCapacityKey;
263 static OSSharedPtr<const OSSymbol>         gIOPMPSMaxCapacityKey;
264 static OSSharedPtr<const OSSymbol>         gIOPMPSDesignCapacityKey;
265 static OSSharedPtr<const OSSymbol>         gIOPMPSTimeRemainingKey;
266 static OSSharedPtr<const OSSymbol>         gIOPMPSAmperageKey;
267 static OSSharedPtr<const OSSymbol>         gIOPMPSVoltageKey;
268 static OSSharedPtr<const OSSymbol>         gIOPMPSCycleCountKey;
269 static OSSharedPtr<const OSSymbol>         gIOPMPSMaxErrKey;
270 static OSSharedPtr<const OSSymbol>         gIOPMPSAdapterInfoKey;
271 static OSSharedPtr<const OSSymbol>         gIOPMPSLocationKey;
272 static OSSharedPtr<const OSSymbol>         gIOPMPSErrorConditionKey;
273 static OSSharedPtr<const OSSymbol>         gIOPMPSManufacturerKey;
274 static OSSharedPtr<const OSSymbol>         gIOPMPSManufactureDateKey;
275 static OSSharedPtr<const OSSymbol>         gIOPMPSModelKey;
276 static OSSharedPtr<const OSSymbol>         gIOPMPSSerialKey;
277 static OSSharedPtr<const OSSymbol>         gIOPMPSLegacyBatteryInfoKey;
278 static OSSharedPtr<const OSSymbol>         gIOPMPSBatteryHealthKey;
279 static OSSharedPtr<const OSSymbol>         gIOPMPSHealthConfidenceKey;
280 static OSSharedPtr<const OSSymbol>         gIOPMPSCapacityEstimatedKey;
281 static OSSharedPtr<const OSSymbol>         gIOPMPSBatteryChargeStatusKey;
282 static OSSharedPtr<const OSSymbol>         gIOPMPSBatteryTemperatureKey;
283 static OSSharedPtr<const OSSymbol>         gIOPMPSAdapterDetailsKey;
284 static OSSharedPtr<const OSSymbol>         gIOPMPSChargerConfigurationKey;
285 static OSSharedPtr<const OSSymbol>         gIOPMPSAdapterDetailsIDKey;
286 static OSSharedPtr<const OSSymbol>         gIOPMPSAdapterDetailsWattsKey;
287 static OSSharedPtr<const OSSymbol>         gIOPMPSAdapterDetailsRevisionKey;
288 static OSSharedPtr<const OSSymbol>         gIOPMPSAdapterDetailsSerialNumberKey;
289 static OSSharedPtr<const OSSymbol>         gIOPMPSAdapterDetailsFamilyKey;
290 static OSSharedPtr<const OSSymbol>         gIOPMPSAdapterDetailsAmperageKey;
291 static OSSharedPtr<const OSSymbol>         gIOPMPSAdapterDetailsDescriptionKey;
292 static OSSharedPtr<const OSSymbol>         gIOPMPSAdapterDetailsPMUConfigurationKey;
293 static OSSharedPtr<const OSSymbol>         gIOPMPSAdapterDetailsSourceIDKey;
294 static OSSharedPtr<const OSSymbol>         gIOPMPSAdapterDetailsErrorFlagsKey;
295 static OSSharedPtr<const OSSymbol>         gIOPMPSAdapterDetailsSharedSourceKey;
296 static OSSharedPtr<const OSSymbol>         gIOPMPSAdapterDetailsCloakedKey;
297 static OSSharedPtr<const OSSymbol>         gIOPMPSInvalidWakeSecondsKey;
298 static OSSharedPtr<const OSSymbol>         gIOPMPSPostChargeWaitSecondsKey;
299 static OSSharedPtr<const OSSymbol>         gIOPMPSPostDishargeWaitSecondsKey;
300 
301 #define kIOSleepSupportedKey        "IOSleepSupported"
302 #define kIOPMSystemCapabilitiesKey  "System Capabilities"
303 #define kIOPMSystemDefaultOverrideKey   "SystemPowerProfileOverrideDict"
304 
305 #define kIORequestWranglerIdleKey   "IORequestIdle"
306 #define kDefaultWranglerIdlePeriod  1000 // in milliseconds
307 
308 #define kIOSleepWakeFailureString   "SleepWakeFailureString"
309 #define kIOEFIBootRomFailureKey     "wake-failure"
310 #define kIOSleepWakeFailurePanic    "SleepWakeFailurePanic"
311 
312 #define kRD_AllPowerSources (kIOPMSupportedOnAC \
313 	                   | kIOPMSupportedOnBatt \
314 	                   | kIOPMSupportedOnUPS)
315 
316 #define kLocalEvalClamshellCommand  (1 << 15)
317 #define kIdleSleepRetryInterval     (3 * 60 * 1000)
318 
319 // Minimum time in milliseconds after AP wake that we allow idle timer to expire.
320 // We impose this minimum to avoid race conditions in the AP wake path where
321 // userspace clients are not able to acquire power assertions before the idle timer expires.
322 #define kMinimumTimeBeforeIdleSleep     1000
323 
324 #define DISPLAY_WRANGLER_PRESENT    (!NO_KERNEL_HID)
325 
326 enum {
327 	kWranglerPowerStateMin   = 0,
328 	kWranglerPowerStateSleep = 2,
329 	kWranglerPowerStateDim   = 3,
330 	kWranglerPowerStateMax   = 4
331 };
332 
333 enum {
334 	OFF_STATE           = 0,
335 	RESTART_STATE       = 1,
336 	SLEEP_STATE         = 2,
337 	AOT_STATE           = 3,
338 	ON_STATE            = 4,
339 	NUM_POWER_STATES
340 };
341 
342 const char *
getPowerStateString(uint32_t state)343 getPowerStateString( uint32_t state )
344 {
345 #define POWER_STATE(x) {(uint32_t) x, #x}
346 
347 	static const IONamedValue powerStates[] = {
348 		POWER_STATE( OFF_STATE ),
349 		POWER_STATE( RESTART_STATE ),
350 		POWER_STATE( SLEEP_STATE ),
351 		POWER_STATE( AOT_STATE ),
352 		POWER_STATE( ON_STATE ),
353 		{ 0, NULL }
354 	};
355 	return IOFindNameForValue(state, powerStates);
356 }
357 
358 #define ON_POWER        kIOPMPowerOn
359 #define RESTART_POWER   kIOPMRestart
360 #define SLEEP_POWER     kIOPMAuxPowerOn
361 
362 static IOPMPowerState
363     ourPowerStates[NUM_POWER_STATES] =
364 {
365 	{   .version                = 1,
366 	    .capabilityFlags        = 0,
367 	    .outputPowerCharacter   = 0,
368 	    .inputPowerRequirement  = 0 },
369 	{   .version                = 1,
370 	    .capabilityFlags        = kIOPMRestartCapability,
371 	    .outputPowerCharacter   = kIOPMRestart,
372 	    .inputPowerRequirement  = RESTART_POWER },
373 	{   .version                = 1,
374 	    .capabilityFlags        = kIOPMSleepCapability,
375 	    .outputPowerCharacter   = kIOPMSleep,
376 	    .inputPowerRequirement  = SLEEP_POWER },
377 	{   .version                = 1,
378 	    .capabilityFlags        = kIOPMAOTCapability,
379 	    .outputPowerCharacter   = kIOPMAOTPower,
380 	    .inputPowerRequirement  = ON_POWER },
381 	{   .version                = 1,
382 	    .capabilityFlags        = kIOPMPowerOn,
383 	    .outputPowerCharacter   = kIOPMPowerOn,
384 	    .inputPowerRequirement  = ON_POWER },
385 };
386 
387 #define kIOPMRootDomainWakeTypeSleepService     "SleepService"
388 #define kIOPMRootDomainWakeTypeMaintenance      "Maintenance"
389 #define kIOPMRootDomainWakeTypeSleepTimer       "SleepTimer"
390 #define kIOPMrootDomainWakeTypeLowBattery       "LowBattery"
391 #define kIOPMRootDomainWakeTypeUser             "User"
392 #define kIOPMRootDomainWakeTypeAlarm            "Alarm"
393 #define kIOPMRootDomainWakeTypeNetwork          "Network"
394 #define kIOPMRootDomainWakeTypeHIDActivity      "HID Activity"
395 #define kIOPMRootDomainWakeTypeNotification     "Notification"
396 #define kIOPMRootDomainWakeTypeHibernateError   "HibernateError"
397 
398 // Special interest that entitles the interested client from receiving
399 // all system messages. Only used by powerd.
400 //
401 #define kIOPMSystemCapabilityInterest       "IOPMSystemCapabilityInterest"
402 
403 // Entitlement required for root domain clients
404 #define kRootDomainEntitlementSetProperty   "com.apple.private.iokit.rootdomain-set-property"
405 
406 #define WAKEEVENT_LOCK()        IOLockLock(wakeEventLock)
407 #define WAKEEVENT_UNLOCK()      IOLockUnlock(wakeEventLock)
408 
409 /*
410  * Aggressiveness
411  */
412 #define AGGRESSIVES_LOCK()      IOLockLock(featuresDictLock)
413 #define AGGRESSIVES_UNLOCK()    IOLockUnlock(featuresDictLock)
414 
415 #define kAggressivesMinValue    1
416 
417 const char *
getAggressivenessTypeString(uint32_t type)418 getAggressivenessTypeString( uint32_t type )
419 {
420 #define AGGRESSIVENESS_TYPE(x) {(uint32_t) x, #x}
421 
422 	static const IONamedValue aggressivenessTypes[] = {
423 		AGGRESSIVENESS_TYPE( kPMGeneralAggressiveness ),
424 		AGGRESSIVENESS_TYPE( kPMMinutesToDim ),
425 		AGGRESSIVENESS_TYPE( kPMMinutesToSpinDown ),
426 		AGGRESSIVENESS_TYPE( kPMMinutesToSleep ),
427 		AGGRESSIVENESS_TYPE( kPMEthernetWakeOnLANSettings ),
428 		AGGRESSIVENESS_TYPE( kPMSetProcessorSpeed ),
429 		AGGRESSIVENESS_TYPE( kPMPowerSource),
430 		AGGRESSIVENESS_TYPE( kPMMotionSensor ),
431 		AGGRESSIVENESS_TYPE( kPMLastAggressivenessType ),
432 		{ 0, NULL }
433 	};
434 	return IOFindNameForValue(type, aggressivenessTypes);
435 }
436 
437 enum {
438 	kAggressivesStateBusy           = 0x01,
439 	kAggressivesStateQuickSpindown  = 0x02
440 };
441 
442 struct AggressivesRecord {
443 	uint32_t    flags;
444 	uint32_t    type;
445 	uint32_t    value;
446 };
447 
448 struct AggressivesRequest {
449 	queue_chain_t           chain;
450 	uint32_t                options;
451 	uint32_t                dataType;
452 	union {
453 		OSSharedPtr<IOService> service;
454 		AggressivesRecord      record;
455 	} data;
456 };
457 
458 enum {
459 	kAggressivesRequestTypeService  = 1,
460 	kAggressivesRequestTypeRecord
461 };
462 
463 enum {
464 	kAggressivesOptionSynchronous          = 0x00000001,
465 	kAggressivesOptionQuickSpindownEnable  = 0x00000100,
466 	kAggressivesOptionQuickSpindownDisable = 0x00000200,
467 	kAggressivesOptionQuickSpindownMask    = 0x00000300
468 };
469 
470 enum {
471 	kAggressivesRecordFlagModified         = 0x00000001,
472 	kAggressivesRecordFlagMinValue         = 0x00000002
473 };
474 
475 // System Sleep Preventers
476 
477 enum {
478 	kPMUserDisabledAllSleep = 1,
479 	kPMSystemRestartBootingInProgress,
480 	kPMConfigPreventSystemSleep,
481 	kPMChildPreventSystemSleep,
482 	kPMCPUAssertion,
483 	kPMPCIUnsupported,
484 	kPMDKNotReady,
485 };
486 
487 const char *
getSystemSleepPreventerString(uint32_t preventer)488 getSystemSleepPreventerString( uint32_t preventer )
489 {
490 #define SYSTEM_SLEEP_PREVENTER(x) {(int) x, #x}
491 	static const IONamedValue systemSleepPreventers[] = {
492 		SYSTEM_SLEEP_PREVENTER( kPMUserDisabledAllSleep ),
493 		SYSTEM_SLEEP_PREVENTER( kPMSystemRestartBootingInProgress ),
494 		SYSTEM_SLEEP_PREVENTER( kPMConfigPreventSystemSleep ),
495 		SYSTEM_SLEEP_PREVENTER( kPMChildPreventSystemSleep ),
496 		SYSTEM_SLEEP_PREVENTER( kPMCPUAssertion ),
497 		SYSTEM_SLEEP_PREVENTER( kPMPCIUnsupported ),
498 		SYSTEM_SLEEP_PREVENTER( kPMDKNotReady ),
499 		{ 0, NULL }
500 	};
501 	return IOFindNameForValue(preventer, systemSleepPreventers);
502 }
503 
504 // gDarkWakeFlags
505 enum {
506 	kDarkWakeFlagPromotionNone       = 0x0000,
507 	kDarkWakeFlagPromotionEarly      = 0x0001, // promote before gfx clamp
508 	kDarkWakeFlagPromotionLate       = 0x0002, // promote after gfx clamp
509 	kDarkWakeFlagPromotionMask       = 0x0003,
510 	kDarkWakeFlagAlarmIsDark         = 0x0100,
511 	kDarkWakeFlagAudioNotSuppressed  = 0x0200,
512 	kDarkWakeFlagUserWakeWorkaround  = 0x1000
513 };
514 
515 // gClamshellFlags
516 // The workaround for 9157444 is enabled at compile time using the
517 // DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY macro and is not represented below.
518 enum {
519 	kClamshell_WAR_38378787 = 0x00000001,
520 	kClamshell_WAR_47715679 = 0x00000002,
521 	kClamshell_WAR_58009435 = 0x00000004
522 };
523 
524 // acceptSystemWakeEvents()
525 enum {
526 	kAcceptSystemWakeEvents_Disable = 0,
527 	kAcceptSystemWakeEvents_Enable,
528 	kAcceptSystemWakeEvents_Reenable
529 };
530 
531 static IOPMrootDomain * gRootDomain;
532 static IORootParent *   gPatriarch;
533 static IONotifier *     gSysPowerDownNotifier = NULL;
534 static UInt32           gSleepOrShutdownPending = 0;
535 static UInt32           gWillShutdown = 0;
536 static UInt32           gPagingOff = 0;
537 static UInt32           gSleepWakeUUIDIsSet = false;
538 static uint32_t         gAggressivesState = 0;
539 uint32_t                gHaltTimeMaxLog;
540 uint32_t                gHaltTimeMaxPanic;
541 IOLock *                gHaltLogLock;
542 static char *           gHaltLog;
543 enum                  { kHaltLogSize = 2048 };
544 static size_t           gHaltLogPos;
545 static uint64_t         gHaltStartTime;
546 static char             gKextNameBuf[64];
547 static size_t           gKextNamePos;
548 static bool             gKextNameEnd;
549 
550 uuid_string_t bootsessionuuid_string;
551 
552 #if defined(XNU_TARGET_OS_OSX)
553 #if DISPLAY_WRANGLER_PRESENT
554 static uint32_t         gDarkWakeFlags = kDarkWakeFlagPromotionNone;
555 #elif defined(__arm64__)
556 // Enable temporary full wake promotion workarounds
557 static uint32_t         gDarkWakeFlags = kDarkWakeFlagUserWakeWorkaround;
558 #else
559 // Enable full wake promotion workarounds
560 static uint32_t         gDarkWakeFlags = kDarkWakeFlagUserWakeWorkaround;
561 #endif
562 #else  /* !defined(XNU_TARGET_OS_OSX) */
563 static uint32_t         gDarkWakeFlags = kDarkWakeFlagPromotionEarly;
564 #endif /* !defined(XNU_TARGET_OS_OSX) */
565 
566 static uint32_t         gNoIdleFlag = 0;
567 static uint32_t         gSleepDisabledFlag = 0;
568 static uint32_t         gSwdPanic = 1;
569 static uint32_t         gSwdSleepTimeout = 0;
570 static uint32_t         gSwdWakeTimeout = 0;
571 static uint32_t         gSwdSleepWakeTimeout = 0;
572 static PMStatsStruct    gPMStats;
573 #if DEVELOPMENT || DEBUG
574 static uint32_t swd_panic_phase;
575 #endif
576 
577 static uint32_t         gClamshellFlags = 0
578 #if defined(__i386__) || defined(__x86_64__)
579     | kClamshell_WAR_58009435
580 #endif
581 ;
582 
583 #if HIBERNATION
584 
585 #if defined(__arm64__)
586 static IOReturn
defaultSleepPolicyHandler(void * ctx,const IOPMSystemSleepPolicyVariables * vars,IOPMSystemSleepParameters * params)587 defaultSleepPolicyHandler(void *ctx, const IOPMSystemSleepPolicyVariables *vars, IOPMSystemSleepParameters *params)
588 {
589 	uint32_t sleepType = kIOPMSleepTypeDeepIdle;
590 
591 	assert(vars->signature == kIOPMSystemSleepPolicySignature);
592 	assert(vars->version == kIOPMSystemSleepPolicyVersion);
593 
594 	// Hibernation enabled and either user forced hibernate or low battery sleep
595 	if ((vars->hibernateMode & kIOHibernateModeOn) &&
596 	    (((vars->hibernateMode & kIOHibernateModeSleep) == 0) ||
597 	    (vars->sleepFactors & kIOPMSleepFactorBatteryLow))) {
598 		sleepType = kIOPMSleepTypeHibernate;
599 	}
600 	params->version = kIOPMSystemSleepParametersVersion;
601 	params->sleepType = sleepType;
602 	return kIOReturnSuccess;
603 }
604 static IOPMSystemSleepPolicyHandler     gSleepPolicyHandler = &defaultSleepPolicyHandler;
605 #else /* defined(__arm64__) */
606 static IOPMSystemSleepPolicyHandler     gSleepPolicyHandler = NULL;
607 #endif /* defined(__arm64__) */
608 
609 static IOPMSystemSleepPolicyVariables * gSleepPolicyVars = NULL;
610 static void *                           gSleepPolicyTarget;
611 #endif
612 
613 struct timeval gIOLastSleepTime;
614 struct timeval gIOLastWakeTime;
615 AbsoluteTime gIOLastWakeAbsTime;
616 AbsoluteTime gIOLastSleepAbsTime;
617 
618 struct timeval gIOLastUserSleepTime;
619 
620 static char gWakeReasonString[128];
621 static char gBootReasonString[80];
622 static char gShutdownReasonString[80];
623 static bool gWakeReasonSysctlRegistered = false;
624 static bool gBootReasonSysctlRegistered = false;
625 static bool gShutdownReasonSysctlRegistered = false;
626 static bool gWillShutdownSysctlRegistered = false;
627 static AbsoluteTime gUserActiveAbsTime;
628 static AbsoluteTime gUserInactiveAbsTime;
629 
630 #if defined(__i386__) || defined(__x86_64__) || (defined(__arm64__) && HIBERNATION)
631 static bool gSpinDumpBufferFull = false;
632 #endif
633 
634 z_stream          swd_zs;
635 vm_offset_t swd_zs_zmem;
636 //size_t swd_zs_zsize;
637 size_t swd_zs_zoffset;
638 #if defined(__i386__) || defined(__x86_64__)
639 IOCPU *currentShutdownTarget = NULL;
640 #endif
641 
642 static unsigned int     gPMHaltBusyCount;
643 static unsigned int     gPMHaltIdleCount;
644 static int              gPMHaltDepth;
645 static uint32_t         gPMHaltMessageType;
646 static IOLock *         gPMHaltLock  = NULL;
647 static OSSharedPtr<OSArray>        gPMHaltArray;
648 static OSSharedPtr<const OSSymbol> gPMHaltClientAcknowledgeKey;
649 static bool             gPMQuiesced;
650 
651 // Constants used as arguments to IOPMrootDomain::informCPUStateChange
652 #define kCPUUnknownIndex    9999999
653 enum {
654 	kInformAC = 0,
655 	kInformLid = 1,
656 	kInformableCount = 2
657 };
658 
659 OSSharedPtr<const OSSymbol> gIOPMStatsResponseTimedOut;
660 OSSharedPtr<const OSSymbol> gIOPMStatsResponseCancel;
661 OSSharedPtr<const OSSymbol> gIOPMStatsResponseSlow;
662 OSSharedPtr<const OSSymbol> gIOPMStatsResponsePrompt;
663 OSSharedPtr<const OSSymbol> gIOPMStatsDriverPSChangeSlow;
664 
665 #define kBadPMFeatureID     0
666 
667 /*
668  * PMSettingHandle
669  * Opaque handle passed to clients of registerPMSettingController()
670  */
671 class PMSettingHandle : public OSObject
672 {
673 	OSDeclareFinalStructors( PMSettingHandle );
674 	friend class PMSettingObject;
675 
676 private:
677 	PMSettingObject *pmso;
678 	void free(void) APPLE_KEXT_OVERRIDE;
679 };
680 
681 /*
682  * PMSettingObject
683  * Internal object to track each PM setting controller
684  */
685 class PMSettingObject : public OSObject
686 {
687 	OSDeclareFinalStructors( PMSettingObject );
688 	friend class IOPMrootDomain;
689 
690 private:
691 	queue_head_t                    calloutQueue;
692 	thread_t                        waitThread;
693 	IOPMrootDomain                  *parent;
694 	PMSettingHandle                 *pmsh;
695 	IOPMSettingControllerCallback   func;
696 	OSObject                        *target;
697 	uintptr_t                       refcon;
698 	OSDataAllocation<uint32_t>      publishedFeatureID;
699 	uint32_t                        settingCount;
700 	bool                            disabled;
701 
702 	void free(void) APPLE_KEXT_OVERRIDE;
703 
704 public:
705 	static PMSettingObject *pmSettingObject(
706 		IOPMrootDomain                  *parent_arg,
707 		IOPMSettingControllerCallback   handler_arg,
708 		OSObject                        *target_arg,
709 		uintptr_t                       refcon_arg,
710 		uint32_t                        supportedPowerSources,
711 		const OSSymbol                  *settings[],
712 		OSObject                        **handle_obj);
713 
714 	IOReturn dispatchPMSetting(const OSSymbol *type, OSObject *object);
715 	void clientHandleFreed(void);
716 };
717 
718 struct PMSettingCallEntry {
719 	queue_chain_t   link;
720 	thread_t        thread;
721 };
722 
723 #define PMSETTING_LOCK()    IOLockLock(settingsCtrlLock)
724 #define PMSETTING_UNLOCK()  IOLockUnlock(settingsCtrlLock)
725 #define PMSETTING_WAIT(p)   IOLockSleep(settingsCtrlLock, p, THREAD_UNINT)
726 #define PMSETTING_WAKEUP(p) IOLockWakeup(settingsCtrlLock, p, true)
727 
728 /*
729  * PMTraceWorker
730  * Internal helper object for logging trace points to RTC
731  * IOPMrootDomain and only IOPMrootDomain should instantiate
732  * exactly one of these.
733  */
734 
735 typedef void (*IOPMTracePointHandler)(
736 	void * target, uint32_t code, uint32_t data );
737 
738 class PMTraceWorker : public OSObject
739 {
740 	OSDeclareDefaultStructors(PMTraceWorker);
741 public:
742 	typedef enum { kPowerChangeStart, kPowerChangeCompleted } change_t;
743 
744 	static OSPtr<PMTraceWorker> tracer( IOPMrootDomain * );
745 	void                        tracePCIPowerChange(change_t, IOService *, uint32_t, uint32_t);
746 	void                        tracePoint(uint8_t phase);
747 	void                        traceDetail(uint32_t detail);
748 	void                        traceComponentWakeProgress(uint32_t component, uint32_t data);
749 	int                         recordTopLevelPCIDevice(IOService *);
750 	void                        RTC_TRACE(void);
751 	virtual bool                serialize(OSSerialize *s) const APPLE_KEXT_OVERRIDE;
752 
753 	IOPMTracePointHandler       tracePointHandler;
754 	void *                      tracePointTarget;
755 	uint64_t                    getPMStatusCode();
756 	uint8_t                     getTracePhase();
757 	uint32_t                    getTraceData();
758 private:
759 	IOPMrootDomain              *owner;
760 	IOLock                      *pmTraceWorkerLock;
761 	OSSharedPtr<OSArray>         pciDeviceBitMappings;
762 
763 	uint8_t                     addedToRegistry;
764 	uint8_t                     tracePhase;
765 	uint32_t                    traceData32;
766 	uint8_t                     loginWindowData;
767 	uint8_t                     coreDisplayData;
768 	uint8_t                     coreGraphicsData;
769 };
770 
771 /*
772  * this should be treated as POD, as it's byte-copied around
773  * and we cannot rely on d'tor firing at the right time
774  */
775 struct PMAssertStruct {
776 	IOPMDriverAssertionID       id;
777 	IOPMDriverAssertionType     assertionBits;
778 	uint64_t                    createdTime;
779 	uint64_t                    modifiedTime;
780 	const OSSymbol              *ownerString;
781 	IOService                   *ownerService;
782 	uint64_t                    registryEntryID;
783 	IOPMDriverAssertionLevel    level;
784 	uint64_t                    assertCPUStartTime;
785 	uint64_t                    assertCPUDuration;
786 };
787 OSDefineValueObjectForDependentType(PMAssertStruct)
788 
789 /*
790  * PMAssertionsTracker
791  * Tracks kernel and user space PM assertions
792  */
793 class PMAssertionsTracker : public OSObject
794 {
795 	OSDeclareFinalStructors(PMAssertionsTracker);
796 public:
797 	static PMAssertionsTracker  *pmAssertionsTracker( IOPMrootDomain * );
798 
799 	IOReturn                    createAssertion(IOPMDriverAssertionType, IOPMDriverAssertionLevel, IOService *, const char *, IOPMDriverAssertionID *);
800 	IOReturn                    releaseAssertion(IOPMDriverAssertionID);
801 	IOReturn                    setAssertionLevel(IOPMDriverAssertionID, IOPMDriverAssertionLevel);
802 	IOReturn                    setUserAssertionLevels(IOPMDriverAssertionType);
803 
804 	OSSharedPtr<OSArray>        copyAssertionsArray(void);
805 	IOPMDriverAssertionType     getActivatedAssertions(void);
806 	IOPMDriverAssertionLevel    getAssertionLevel(IOPMDriverAssertionType);
807 
808 	IOReturn                    handleCreateAssertion(OSValueObject<PMAssertStruct> *);
809 	IOReturn                    handleReleaseAssertion(IOPMDriverAssertionID);
810 	IOReturn                    handleSetAssertionLevel(IOPMDriverAssertionID, IOPMDriverAssertionLevel);
811 	IOReturn                    handleSetUserAssertionLevels(void * arg0);
812 	void                        publishProperties(void);
813 	void                        reportCPUBitAccounting(void);
814 	PMAssertStruct              *detailsForID(IOPMDriverAssertionID, int *);
815 
816 private:
817 	uint32_t                    tabulateProducerCount;
818 	uint32_t                    tabulateConsumerCount;
819 
820 	uint64_t                    maxAssertCPUDuration;
821 	uint64_t                    maxAssertCPUEntryId;
822 
823 	void                        tabulate(void);
824 	void                        updateCPUBitAccounting(PMAssertStruct * assertStruct);
825 
826 	IOPMrootDomain              *owner;
827 	OSSharedPtr<OSArray>        assertionsArray;
828 	IOLock                      *assertionsArrayLock;
829 	IOPMDriverAssertionID       issuingUniqueID __attribute__((aligned(8)));/* aligned for atomic access */
830 	IOPMDriverAssertionType     assertionsKernel;
831 	IOPMDriverAssertionType     assertionsUser;
832 	IOPMDriverAssertionType     assertionsCombined;
833 };
834 
835 OSDefineMetaClassAndFinalStructors(PMAssertionsTracker, OSObject);
836 
837 /*
838  * PMHaltWorker
839  * Internal helper object for Shutdown/Restart notifications.
840  */
841 #define kPMHaltMaxWorkers   8
842 #define kPMHaltTimeoutMS    100
843 
844 class PMHaltWorker : public OSObject
845 {
846 	OSDeclareFinalStructors( PMHaltWorker );
847 
848 public:
849 	IOService *  service;// service being worked on
850 	AbsoluteTime startTime; // time when work started
851 	int          depth;  // work on nubs at this PM-tree depth
852 	int          visits; // number of nodes visited (debug)
853 	IOLock *     lock;
854 	bool         timeout;// service took too long
855 
856 	static  PMHaltWorker * worker( void );
857 	static  void main( void * arg, wait_result_t waitResult );
858 	static  void work( PMHaltWorker * me );
859 	static  void checkTimeout( PMHaltWorker * me, AbsoluteTime * now );
860 	virtual void free( void ) APPLE_KEXT_OVERRIDE;
861 };
862 
OSDefineMetaClassAndFinalStructors(PMHaltWorker,OSObject)863 OSDefineMetaClassAndFinalStructors( PMHaltWorker, OSObject )
864 
865 
866 #define super IOService
867 OSDefineMetaClassAndFinalStructors(IOPMrootDomain, IOService)
868 
869 boolean_t
870 IOPMRootDomainGetWillShutdown(void)
871 {
872 	return gWillShutdown != 0;
873 }
874 
875 static void
IOPMRootDomainWillShutdown(void)876 IOPMRootDomainWillShutdown(void)
877 {
878 	if (OSCompareAndSwap(0, 1, &gWillShutdown)) {
879 		IOService::willShutdown();
880 		for (int i = 0; i < 100; i++) {
881 			if (OSCompareAndSwap(0, 1, &gSleepOrShutdownPending)) {
882 				break;
883 			}
884 			IOSleep( 100 );
885 		}
886 	}
887 }
888 
889 extern "C" IONotifier *
registerSleepWakeInterest(IOServiceInterestHandler handler,void * self,void * ref)890 registerSleepWakeInterest(IOServiceInterestHandler handler, void * self, void * ref)
891 {
892 	return gRootDomain->registerInterest( gIOGeneralInterest, handler, self, ref ).detach();
893 }
894 
895 extern "C" IONotifier *
registerPrioritySleepWakeInterest(IOServiceInterestHandler handler,void * self,void * ref)896 registerPrioritySleepWakeInterest(IOServiceInterestHandler handler, void * self, void * ref)
897 {
898 	return gRootDomain->registerInterest( gIOPriorityPowerStateInterest, handler, self, ref ).detach();
899 }
900 
901 extern "C" IOReturn
acknowledgeSleepWakeNotification(void * PMrefcon)902 acknowledgeSleepWakeNotification(void * PMrefcon)
903 {
904 	return gRootDomain->allowPowerChange((unsigned long)PMrefcon );
905 }
906 
907 extern "C" IOReturn
vetoSleepWakeNotification(void * PMrefcon)908 vetoSleepWakeNotification(void * PMrefcon)
909 {
910 	return gRootDomain->cancelPowerChange((unsigned long)PMrefcon );
911 }
912 
913 extern "C" IOReturn
rootDomainRestart(void)914 rootDomainRestart( void )
915 {
916 	return gRootDomain->restartSystem();
917 }
918 
919 extern "C" IOReturn
rootDomainShutdown(void)920 rootDomainShutdown( void )
921 {
922 	return gRootDomain->shutdownSystem();
923 }
924 
925 static void
halt_log_putc(char c)926 halt_log_putc(char c)
927 {
928 	if (gHaltLogPos >= (kHaltLogSize - 2)) {
929 		return;
930 	}
931 	gHaltLog[gHaltLogPos++] = c;
932 }
933 
934 extern "C" void
935 _doprnt_log(const char     *fmt,
936     va_list                 *argp,
937     void                    (*putc)(char),
938     int                     radix);
939 
940 static int
halt_log(const char * fmt,...)941 halt_log(const char *fmt, ...)
942 {
943 	va_list listp;
944 
945 	va_start(listp, fmt);
946 	_doprnt_log(fmt, &listp, &halt_log_putc, 16);
947 	va_end(listp);
948 
949 	return 0;
950 }
951 
952 extern "C" void
halt_log_enter(const char * what,const void * pc,uint64_t time)953 halt_log_enter(const char * what, const void * pc, uint64_t time)
954 {
955 	uint64_t nano, millis;
956 
957 	if (!gHaltLog) {
958 		return;
959 	}
960 	absolutetime_to_nanoseconds(time, &nano);
961 	millis = nano / NSEC_PER_MSEC;
962 	if (millis < 100) {
963 		return;
964 	}
965 
966 	IOLockLock(gHaltLogLock);
967 	if (pc) {
968 		halt_log("%s: %qd ms @ 0x%lx, ", what, millis, VM_KERNEL_UNSLIDE(pc));
969 		OSKext::printKextsInBacktrace((vm_offset_t *) &pc, 1, &halt_log,
970 		    OSKext::kPrintKextsLock | OSKext::kPrintKextsUnslide | OSKext::kPrintKextsTerse);
971 	} else {
972 		halt_log("%s: %qd ms\n", what, millis);
973 	}
974 
975 	gHaltLog[gHaltLogPos] = 0;
976 	IOLockUnlock(gHaltLogLock);
977 }
978 
979 extern  uint32_t                           gFSState;
980 
981 extern "C" void
IOSystemShutdownNotification(int howto,int stage)982 IOSystemShutdownNotification(int howto, int stage)
983 {
984 	uint64_t startTime;
985 
986 	if (kIOSystemShutdownNotificationStageRootUnmount == stage) {
987 #if defined(XNU_TARGET_OS_OSX)
988 		uint64_t nano, millis;
989 		startTime = mach_absolute_time();
990 		IOService::getPlatform()->waitQuiet(30 * NSEC_PER_SEC);
991 		absolutetime_to_nanoseconds(mach_absolute_time() - startTime, &nano);
992 		millis = nano / NSEC_PER_MSEC;
993 		if (gHaltTimeMaxLog && (millis >= gHaltTimeMaxLog)) {
994 			printf("waitQuiet() for unmount %qd ms\n", millis);
995 		}
996 #endif /* defined(XNU_TARGET_OS_OSX) */
997 		return;
998 	}
999 
1000 	if (kIOSystemShutdownNotificationTerminateDEXTs == stage) {
1001 		uint64_t nano, millis;
1002 		startTime = mach_absolute_time();
1003 		IOServicePH::systemHalt(howto);
1004 		absolutetime_to_nanoseconds(mach_absolute_time() - startTime, &nano);
1005 		millis = nano / NSEC_PER_MSEC;
1006 		if (true || (gHaltTimeMaxLog && (millis >= gHaltTimeMaxLog))) {
1007 			printf("IOServicePH::systemHalt took %qd ms\n", millis);
1008 		}
1009 		return;
1010 	}
1011 
1012 	assert(kIOSystemShutdownNotificationStageProcessExit == stage);
1013 
1014 	IOLockLock(gHaltLogLock);
1015 	if (!gHaltLog) {
1016 		gHaltLog = IONewData(char, (vm_size_t)kHaltLogSize);
1017 		gHaltStartTime = mach_absolute_time();
1018 		if (gHaltLog) {
1019 			halt_log_putc('\n');
1020 		}
1021 	}
1022 	IOLockUnlock(gHaltLogLock);
1023 
1024 	startTime = mach_absolute_time();
1025 	IOPMRootDomainWillShutdown();
1026 	halt_log_enter("IOPMRootDomainWillShutdown", NULL, mach_absolute_time() - startTime);
1027 #if HIBERNATION
1028 	startTime = mach_absolute_time();
1029 	IOHibernateSystemPostWake(true);
1030 	halt_log_enter("IOHibernateSystemPostWake", NULL, mach_absolute_time() - startTime);
1031 #endif
1032 	if (OSCompareAndSwap(0, 1, &gPagingOff)) {
1033 		gRootDomain->handlePlatformHaltRestart(kPEPagingOff);
1034 	}
1035 }
1036 
1037 extern "C" int sync_internal(void);
1038 
1039 /*
1040  *  A device is always in the highest power state which satisfies its driver,
1041  *  its policy-maker, and any power children it has, but within the constraint
1042  *  of the power state provided by its parent.  The driver expresses its desire by
1043  *  calling changePowerStateTo(), the policy-maker expresses its desire by calling
1044  *  changePowerStateToPriv(), and the children express their desires by calling
1045  *  requestPowerDomainState().
1046  *
1047  *  The Root Power Domain owns the policy for idle and demand sleep for the system.
1048  *  It is a power-managed IOService just like the others in the system.
1049  *  It implements several power states which map to what we see as Sleep and On.
1050  *
1051  *  The sleep policy is as follows:
1052  *  1. Sleep is prevented if the case is open so that nobody will think the machine
1053  *  is off and plug/unplug cards.
1054  *  2. Sleep is prevented if the sleep timeout slider in the prefs panel is zero.
1055  *  3. System cannot Sleep if some object in the tree is in a power state marked
1056  *  kIOPMPreventSystemSleep.
1057  *
1058  *  These three conditions are enforced using the "driver clamp" by calling
1059  *  changePowerStateTo(). For example, if the case is opened,
1060  *  changePowerStateTo(ON_STATE) is called to hold the system on regardless
1061  *  of the desires of the children of the root or the state of the other clamp.
1062  *
1063  *  Demand Sleep is initiated by pressing the front panel power button, closing
1064  *  the clamshell, or selecting the menu item. In this case the root's parent
1065  *  actually initiates the power state change so that the root domain has no
1066  *  choice and does not give applications the opportunity to veto the change.
1067  *
1068  *  Idle Sleep occurs if no objects in the tree are in a state marked
1069  *  kIOPMPreventIdleSleep.  When this is true, the root's children are not holding
1070  *  the root on, so it sets the "policy-maker clamp" by calling
1071  *  changePowerStateToPriv(ON_STATE) to hold itself on until the sleep timer expires.
1072  *  This timer is set for the difference between the sleep timeout slider and the
1073  *  display dim timeout slider. When the timer expires, it releases its clamp and
1074  *  now nothing is holding it awake, so it falls asleep.
1075  *
1076  *  Demand sleep is prevented when the system is booting.  When preferences are
1077  *  transmitted by the loginwindow at the end of boot, a flag is cleared,
1078  *  and this allows subsequent Demand Sleep.
1079  */
1080 
1081 //******************************************************************************
1082 
1083 IOPMrootDomain *
construct(void)1084 IOPMrootDomain::construct( void )
1085 {
1086 	IOPMrootDomain  *root;
1087 
1088 	root = new IOPMrootDomain;
1089 	if (root) {
1090 		root->init();
1091 	}
1092 
1093 	return root;
1094 }
1095 
1096 //******************************************************************************
1097 // updateConsoleUsersCallout
1098 //
1099 //******************************************************************************
1100 
1101 static void
updateConsoleUsersCallout(thread_call_param_t p0,thread_call_param_t p1)1102 updateConsoleUsersCallout(thread_call_param_t p0, thread_call_param_t p1)
1103 {
1104 	IOPMrootDomain * rootDomain = (IOPMrootDomain *) p0;
1105 	rootDomain->updateConsoleUsers();
1106 }
1107 
1108 void
updateConsoleUsers(void)1109 IOPMrootDomain::updateConsoleUsers(void)
1110 {
1111 	IOService::updateConsoleUsers(NULL, kIOMessageSystemHasPoweredOn);
1112 	updateTasksSuspend(kTasksSuspendUnsuspended, kTasksSuspendNoChange);
1113 }
1114 
1115 bool
updateTasksSuspend(int newTasksSuspended,int newAOTTasksSuspended)1116 IOPMrootDomain::updateTasksSuspend(int newTasksSuspended, int newAOTTasksSuspended)
1117 {
1118 	bool newSuspend;
1119 
1120 	WAKEEVENT_LOCK();
1121 	if (newTasksSuspended != kTasksSuspendNoChange) {
1122 		tasksSuspended = (newTasksSuspended != kTasksSuspendUnsuspended);
1123 	}
1124 	if (newAOTTasksSuspended != kTasksSuspendNoChange) {
1125 		_aotTasksSuspended = (newAOTTasksSuspended != kTasksSuspendUnsuspended);
1126 	}
1127 	newSuspend = (tasksSuspended || _aotTasksSuspended);
1128 	if (newSuspend == tasksSuspendState) {
1129 		WAKEEVENT_UNLOCK();
1130 		return false;
1131 	}
1132 	tasksSuspendState = newSuspend;
1133 	WAKEEVENT_UNLOCK();
1134 	tasks_system_suspend(newSuspend);
1135 	return true;
1136 }
1137 
1138 //******************************************************************************
1139 
1140 static void
disk_sync_callout(thread_call_param_t p0,thread_call_param_t p1)1141 disk_sync_callout( thread_call_param_t p0, thread_call_param_t p1 )
1142 {
1143 	IOPMrootDomain * rootDomain = (IOPMrootDomain *) p0;
1144 	uint32_t    notifyRef  = (uint32_t)(uintptr_t) p1;
1145 	uint32_t    powerState = rootDomain->getPowerState();
1146 
1147 	DLOG("disk_sync_callout ps=%u\n", powerState);
1148 
1149 	if (ON_STATE == powerState) {
1150 		sync_internal();
1151 
1152 #if HIBERNATION
1153 		// Block sleep until trim issued on previous wake path is completed.
1154 		IOHibernateSystemPostWake(true);
1155 #endif
1156 	}
1157 #if HIBERNATION
1158 	else {
1159 		IOHibernateSystemPostWake(false);
1160 
1161 		rootDomain->sleepWakeDebugSaveSpinDumpFile();
1162 	}
1163 #endif
1164 
1165 	rootDomain->allowPowerChange(notifyRef);
1166 	DLOG("disk_sync_callout finish\n");
1167 }
1168 
1169 //******************************************************************************
1170 static UInt32
computeDeltaTimeMS(const AbsoluteTime * startTime,AbsoluteTime * elapsedTime)1171 computeDeltaTimeMS( const AbsoluteTime * startTime, AbsoluteTime * elapsedTime )
1172 {
1173 	AbsoluteTime    endTime;
1174 	UInt64          nano = 0;
1175 
1176 	clock_get_uptime(&endTime);
1177 	if (CMP_ABSOLUTETIME(&endTime, startTime) <= 0) {
1178 		*elapsedTime = 0;
1179 	} else {
1180 		SUB_ABSOLUTETIME(&endTime, startTime);
1181 		absolutetime_to_nanoseconds(endTime, &nano);
1182 		*elapsedTime = endTime;
1183 	}
1184 
1185 	return (UInt32)(nano / NSEC_PER_MSEC);
1186 }
1187 
1188 //******************************************************************************
1189 
1190 static int
1191 sysctl_sleepwaketime SYSCTL_HANDLER_ARGS
1192 {
1193 	struct timeval *swt = (struct timeval *)arg1;
1194 	struct proc *p = req->p;
1195 
1196 	if (p == kernproc) {
1197 		return sysctl_io_opaque(req, swt, sizeof(*swt), NULL);
1198 	} else if (proc_is64bit(p)) {
1199 		struct user64_timeval t = {};
1200 		t.tv_sec = swt->tv_sec;
1201 		t.tv_usec = swt->tv_usec;
1202 		return sysctl_io_opaque(req, &t, sizeof(t), NULL);
1203 	} else {
1204 		struct user32_timeval t = {};
1205 		t.tv_sec = (typeof(t.tv_sec))swt->tv_sec;
1206 		t.tv_usec = swt->tv_usec;
1207 		return sysctl_io_opaque(req, &t, sizeof(t), NULL);
1208 	}
1209 }
1210 
1211 static SYSCTL_PROC(_kern, OID_AUTO, sleeptime,
1212     CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
1213     &gIOLastUserSleepTime, 0, sysctl_sleepwaketime, "S,timeval", "");
1214 
1215 static SYSCTL_PROC(_kern, OID_AUTO, waketime,
1216     CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
1217     &gIOLastWakeTime, 0, sysctl_sleepwaketime, "S,timeval", "");
1218 
1219 SYSCTL_QUAD(_kern, OID_AUTO, wake_abs_time, CTLFLAG_RD | CTLFLAG_LOCKED, &gIOLastWakeAbsTime, "");
1220 SYSCTL_QUAD(_kern, OID_AUTO, sleep_abs_time, CTLFLAG_RD | CTLFLAG_LOCKED, &gIOLastSleepAbsTime, "");
1221 SYSCTL_QUAD(_kern, OID_AUTO, useractive_abs_time, CTLFLAG_RD | CTLFLAG_LOCKED, &gUserActiveAbsTime, "");
1222 SYSCTL_QUAD(_kern, OID_AUTO, userinactive_abs_time, CTLFLAG_RD | CTLFLAG_LOCKED, &gUserInactiveAbsTime, "");
1223 
1224 static int
1225 sysctl_willshutdown SYSCTL_HANDLER_ARGS
1226 {
1227 	int new_value, changed, error;
1228 
1229 	if (!gWillShutdownSysctlRegistered) {
1230 		return ENOENT;
1231 	}
1232 
1233 	error = sysctl_io_number(req, gWillShutdown, sizeof(int), &new_value, &changed);
1234 	if (changed) {
1235 		if (!gWillShutdown && (new_value == 1)) {
1236 			IOPMRootDomainWillShutdown();
1237 		} else {
1238 			error = EINVAL;
1239 		}
1240 	}
1241 	return error;
1242 }
1243 
1244 static SYSCTL_PROC(_kern, OID_AUTO, willshutdown,
1245     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
1246     NULL, 0, sysctl_willshutdown, "I", "");
1247 
1248 #if defined(XNU_TARGET_OS_OSX)
1249 
1250 static int
sysctl_progressmeterenable(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)1251 sysctl_progressmeterenable
1252 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
1253 {
1254 	int error;
1255 	int new_value, changed;
1256 
1257 	error = sysctl_io_number(req, vc_progressmeter_enable, sizeof(int), &new_value, &changed);
1258 
1259 	if (changed) {
1260 		vc_enable_progressmeter(new_value);
1261 	}
1262 
1263 	return error;
1264 }
1265 
1266 static int
sysctl_progressmeter(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)1267 sysctl_progressmeter
1268 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
1269 {
1270 	int error;
1271 	int new_value, changed;
1272 
1273 	error = sysctl_io_number(req, vc_progressmeter_value, sizeof(int), &new_value, &changed);
1274 
1275 	if (changed) {
1276 		vc_set_progressmeter(new_value);
1277 	}
1278 
1279 	return error;
1280 }
1281 
1282 static SYSCTL_PROC(_kern, OID_AUTO, progressmeterenable,
1283     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
1284     NULL, 0, sysctl_progressmeterenable, "I", "");
1285 
1286 static SYSCTL_PROC(_kern, OID_AUTO, progressmeter,
1287     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
1288     NULL, 0, sysctl_progressmeter, "I", "");
1289 
1290 #endif /* defined(XNU_TARGET_OS_OSX) */
1291 
1292 
1293 
1294 static int
sysctl_consoleoptions(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)1295 sysctl_consoleoptions
1296 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
1297 {
1298 	int error, changed;
1299 	uint32_t new_value;
1300 
1301 	error = sysctl_io_number(req, vc_user_options.options, sizeof(uint32_t), &new_value, &changed);
1302 
1303 	if (changed) {
1304 		vc_user_options.options = new_value;
1305 	}
1306 
1307 	return error;
1308 }
1309 
1310 static SYSCTL_PROC(_kern, OID_AUTO, consoleoptions,
1311     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
1312     NULL, 0, sysctl_consoleoptions, "I", "");
1313 
1314 
1315 static int
1316 sysctl_progressoptions SYSCTL_HANDLER_ARGS
1317 {
1318 	return sysctl_io_opaque(req, &vc_user_options, sizeof(vc_user_options), NULL);
1319 }
1320 
1321 static SYSCTL_PROC(_kern, OID_AUTO, progressoptions,
1322     CTLTYPE_STRUCT | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED | CTLFLAG_ANYBODY,
1323     NULL, 0, sysctl_progressoptions, "S,vc_progress_user_options", "");
1324 
1325 
1326 static int
1327 sysctl_wakereason SYSCTL_HANDLER_ARGS
1328 {
1329 	char wr[sizeof(gWakeReasonString)];
1330 
1331 	wr[0] = '\0';
1332 	if (gRootDomain && gWakeReasonSysctlRegistered) {
1333 		gRootDomain->copyWakeReasonString(wr, sizeof(wr));
1334 	} else {
1335 		return ENOENT;
1336 	}
1337 
1338 	return sysctl_io_string(req, wr, 0, 0, NULL);
1339 }
1340 
1341 SYSCTL_PROC(_kern, OID_AUTO, wakereason,
1342     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
1343     NULL, 0, sysctl_wakereason, "A", "wakereason");
1344 
1345 static int
1346 sysctl_bootreason SYSCTL_HANDLER_ARGS
1347 {
1348 	if (!os_atomic_load(&gBootReasonSysctlRegistered, acquire)) {
1349 		return ENOENT;
1350 	}
1351 
1352 	return sysctl_io_string(req, gBootReasonString, 0, 0, NULL);
1353 }
1354 
1355 SYSCTL_PROC(_kern, OID_AUTO, bootreason,
1356     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
1357     NULL, 0, sysctl_bootreason, "A", "");
1358 
1359 static int
1360 sysctl_shutdownreason SYSCTL_HANDLER_ARGS
1361 {
1362 	char sr[sizeof(gShutdownReasonString)];
1363 
1364 	sr[0] = '\0';
1365 	if (gRootDomain && gShutdownReasonSysctlRegistered) {
1366 		gRootDomain->copyShutdownReasonString(sr, sizeof(sr));
1367 	} else {
1368 		return ENOENT;
1369 	}
1370 
1371 	return sysctl_io_string(req, sr, 0, 0, NULL);
1372 }
1373 
1374 SYSCTL_PROC(_kern, OID_AUTO, shutdownreason,
1375     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
1376     NULL, 0, sysctl_shutdownreason, "A", "shutdownreason");
1377 
1378 static int
1379 sysctl_targettype SYSCTL_HANDLER_ARGS
1380 {
1381 	IOService * root;
1382 	OSSharedPtr<OSObject>  obj;
1383 	OSData *    data;
1384 	char        tt[32];
1385 
1386 	tt[0] = '\0';
1387 	root = IOService::getServiceRoot();
1388 	if (root && (obj = root->copyProperty(gIODTTargetTypeKey))) {
1389 		if ((data = OSDynamicCast(OSData, obj.get()))) {
1390 			strlcpy(tt, (const char *) data->getBytesNoCopy(), sizeof(tt));
1391 		}
1392 	}
1393 	return sysctl_io_string(req, tt, 0, 0, NULL);
1394 }
1395 
1396 SYSCTL_PROC(_hw, OID_AUTO, targettype,
1397     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
1398     NULL, 0, sysctl_targettype, "A", "targettype");
1399 
1400 static SYSCTL_INT(_debug, OID_AUTO, noidle, CTLFLAG_RW, &gNoIdleFlag, 0, "");
1401 static SYSCTL_INT(_debug, OID_AUTO, swd_sleep_timeout, CTLFLAG_RW, &gSwdSleepTimeout, 0, "");
1402 static SYSCTL_INT(_debug, OID_AUTO, swd_wake_timeout, CTLFLAG_RW, &gSwdWakeTimeout, 0, "");
1403 static SYSCTL_INT(_debug, OID_AUTO, swd_timeout, CTLFLAG_RW, &gSwdSleepWakeTimeout, 0, "");
1404 static SYSCTL_INT(_debug, OID_AUTO, swd_panic, CTLFLAG_RW, &gSwdPanic, 0, "");
1405 #if DEVELOPMENT || DEBUG
1406 static SYSCTL_INT(_debug, OID_AUTO, swd_panic_phase, CTLFLAG_RW, &swd_panic_phase, 0, "");
1407 #if defined(XNU_TARGET_OS_OSX)
1408 static SYSCTL_INT(_debug, OID_AUTO, clamshell, CTLFLAG_RW, &gClamshellFlags, 0, "");
1409 static SYSCTL_INT(_debug, OID_AUTO, darkwake, CTLFLAG_RW, &gDarkWakeFlags, 0, "");
1410 #endif /* defined(XNU_TARGET_OS_OSX) */
1411 #endif /* DEVELOPMENT || DEBUG */
1412 
1413 //******************************************************************************
1414 // AOT
1415 
1416 static int
1417 sysctl_aotmetrics SYSCTL_HANDLER_ARGS
1418 {
1419 	if (NULL == gRootDomain) {
1420 		return ENOENT;
1421 	}
1422 	if (NULL == gRootDomain->_aotMetrics) {
1423 		IOPMAOTMetrics nullMetrics = {};
1424 		return sysctl_io_opaque(req, &nullMetrics, sizeof(IOPMAOTMetrics), NULL);
1425 	}
1426 	return sysctl_io_opaque(req, gRootDomain->_aotMetrics, sizeof(IOPMAOTMetrics), NULL);
1427 }
1428 
1429 static SYSCTL_PROC(_kern, OID_AUTO, aotmetrics,
1430     CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED | CTLFLAG_ANYBODY,
1431     NULL, 0, sysctl_aotmetrics, "S,IOPMAOTMetrics", "");
1432 
1433 
1434 static int
update_aotmode(uint32_t mode)1435 update_aotmode(uint32_t mode)
1436 {
1437 	int result;
1438 
1439 	if (!gIOPMWorkLoop) {
1440 		return ENOENT;
1441 	}
1442 	result = gIOPMWorkLoop->runActionBlock(^IOReturn (void) {
1443 		unsigned int oldCount;
1444 
1445 		if (mode) {
1446 		        gRootDomain->initAOTMetrics();
1447 		}
1448 
1449 		oldCount = gRootDomain->idleSleepPreventersCount();
1450 		gRootDomain->_aotMode = (mode & kIOPMAOTModeMask);
1451 		gRootDomain->updatePreventIdleSleepListInternal(NULL, false, oldCount);
1452 		return 0;
1453 	});
1454 	return result;
1455 }
1456 
1457 static int
sysctl_aotmodebits(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)1458 sysctl_aotmodebits
1459 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
1460 {
1461 	int error, changed;
1462 	uint32_t new_value;
1463 
1464 	if (NULL == gRootDomain) {
1465 		return ENOENT;
1466 	}
1467 	error = sysctl_io_number(req, gRootDomain->_aotMode, sizeof(uint32_t), &new_value, &changed);
1468 	if (changed && gIOPMWorkLoop) {
1469 		error = update_aotmode(new_value);
1470 	}
1471 
1472 	return error;
1473 }
1474 
1475 static SYSCTL_PROC(_kern, OID_AUTO, aotmodebits,
1476     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
1477     NULL, 0, sysctl_aotmodebits, "I", "");
1478 
1479 static int
sysctl_aotmode(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)1480 sysctl_aotmode
1481 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
1482 {
1483 	int error, changed;
1484 	uint32_t new_value;
1485 
1486 	if (NULL == gRootDomain) {
1487 		return ENOENT;
1488 	}
1489 	error = sysctl_io_number(req, gRootDomain->_aotMode, sizeof(uint32_t), &new_value, &changed);
1490 	if (changed && gIOPMWorkLoop) {
1491 		if (new_value) {
1492 			new_value = kIOPMAOTModeDefault; // & ~kIOPMAOTModeRespectTimers;
1493 		}
1494 		error = update_aotmode(new_value);
1495 	}
1496 
1497 	return error;
1498 }
1499 
1500 static SYSCTL_PROC(_kern, OID_AUTO, aotmode,
1501     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED | CTLFLAG_ANYBODY,
1502     NULL, 0, sysctl_aotmode, "I", "");
1503 
1504 //******************************************************************************
1505 
1506 static OSSharedPtr<const OSSymbol> gIOPMSettingAutoWakeCalendarKey;
1507 static OSSharedPtr<const OSSymbol> gIOPMSettingAutoWakeSecondsKey;
1508 static OSSharedPtr<const OSSymbol> gIOPMSettingAutoPowerCalendarKey;
1509 static OSSharedPtr<const OSSymbol> gIOPMSettingAutoPowerSecondsKey;
1510 static OSSharedPtr<const OSSymbol> gIOPMSettingDebugWakeRelativeKey;
1511 static OSSharedPtr<const OSSymbol> gIOPMSettingDebugPowerRelativeKey;
1512 static OSSharedPtr<const OSSymbol> gIOPMSettingMaintenanceWakeCalendarKey;
1513 static OSSharedPtr<const OSSymbol> gIOPMSettingSleepServiceWakeCalendarKey;
1514 static OSSharedPtr<const OSSymbol> gIOPMSettingSilentRunningKey;
1515 static OSSharedPtr<const OSSymbol> gIOPMUserTriggeredFullWakeKey;
1516 static OSSharedPtr<const OSSymbol> gIOPMUserIsActiveKey;
1517 static OSSharedPtr<const OSSymbol> gIOPMSettingLowLatencyAudioModeKey;
1518 
1519 //******************************************************************************
1520 // start
1521 //
1522 //******************************************************************************
1523 
1524 #define kRootDomainSettingsCount           20
1525 #define kRootDomainNoPublishSettingsCount  4
1526 
1527 bool
start(IOService * nub)1528 IOPMrootDomain::start( IOService * nub )
1529 {
1530 	OSSharedPtr<OSIterator>      psIterator;
1531 	OSSharedPtr<OSDictionary>    tmpDict;
1532 
1533 	super::start(nub);
1534 
1535 	gRootDomain = this;
1536 	gIOPMSettingAutoWakeCalendarKey = OSSymbol::withCString(kIOPMSettingAutoWakeCalendarKey);
1537 	gIOPMSettingAutoWakeSecondsKey = OSSymbol::withCString(kIOPMSettingAutoWakeSecondsKey);
1538 	gIOPMSettingAutoPowerCalendarKey = OSSymbol::withCString(kIOPMSettingAutoPowerCalendarKey);
1539 	gIOPMSettingAutoPowerSecondsKey = OSSymbol::withCString(kIOPMSettingAutoPowerSecondsKey);
1540 	gIOPMSettingDebugWakeRelativeKey = OSSymbol::withCString(kIOPMSettingDebugWakeRelativeKey);
1541 	gIOPMSettingDebugPowerRelativeKey = OSSymbol::withCString(kIOPMSettingDebugPowerRelativeKey);
1542 	gIOPMSettingMaintenanceWakeCalendarKey = OSSymbol::withCString(kIOPMSettingMaintenanceWakeCalendarKey);
1543 	gIOPMSettingSleepServiceWakeCalendarKey = OSSymbol::withCString(kIOPMSettingSleepServiceWakeCalendarKey);
1544 	gIOPMSettingSilentRunningKey = OSSymbol::withCStringNoCopy(kIOPMSettingSilentRunningKey);
1545 	gIOPMUserTriggeredFullWakeKey = OSSymbol::withCStringNoCopy(kIOPMUserTriggeredFullWakeKey);
1546 	gIOPMUserIsActiveKey = OSSymbol::withCStringNoCopy(kIOPMUserIsActiveKey);
1547 	gIOPMSettingLowLatencyAudioModeKey = OSSymbol::withCStringNoCopy(kIOPMSettingLowLatencyAudioModeKey);
1548 
1549 	gIOPMStatsResponseTimedOut = OSSymbol::withCString(kIOPMStatsResponseTimedOut);
1550 	gIOPMStatsResponseCancel = OSSymbol::withCString(kIOPMStatsResponseCancel);
1551 	gIOPMStatsResponseSlow = OSSymbol::withCString(kIOPMStatsResponseSlow);
1552 	gIOPMStatsResponsePrompt = OSSymbol::withCString(kIOPMStatsResponsePrompt);
1553 	gIOPMStatsDriverPSChangeSlow = OSSymbol::withCString(kIOPMStatsDriverPSChangeSlow);
1554 
1555 	sleepSupportedPEFunction = OSSymbol::withCString("IOPMSetSleepSupported");
1556 	sleepMessagePEFunction = OSSymbol::withCString("IOPMSystemSleepMessage");
1557 	gIOPMWakeTypeUserKey = OSSymbol::withCStringNoCopy(kIOPMRootDomainWakeTypeUser);
1558 
1559 	OSSharedPtr<const OSSymbol> settingsArr[kRootDomainSettingsCount] =
1560 	{
1561 		OSSymbol::withCString(kIOPMSettingSleepOnPowerButtonKey),
1562 		gIOPMSettingAutoWakeSecondsKey,
1563 		gIOPMSettingAutoPowerSecondsKey,
1564 		gIOPMSettingAutoWakeCalendarKey,
1565 		gIOPMSettingAutoPowerCalendarKey,
1566 		gIOPMSettingDebugWakeRelativeKey,
1567 		gIOPMSettingDebugPowerRelativeKey,
1568 		OSSymbol::withCString(kIOPMSettingWakeOnRingKey),
1569 		OSSymbol::withCString(kIOPMSettingRestartOnPowerLossKey),
1570 		OSSymbol::withCString(kIOPMSettingWakeOnClamshellKey),
1571 		OSSymbol::withCString(kIOPMSettingWakeOnACChangeKey),
1572 		OSSymbol::withCString(kIOPMSettingTimeZoneOffsetKey),
1573 		OSSymbol::withCString(kIOPMSettingDisplaySleepUsesDimKey),
1574 		OSSymbol::withCString(kIOPMSettingMobileMotionModuleKey),
1575 		OSSymbol::withCString(kIOPMSettingGraphicsSwitchKey),
1576 		OSSymbol::withCString(kIOPMStateConsoleShutdown),
1577 		OSSymbol::withCString(kIOPMSettingProModeControl),
1578 		OSSymbol::withCString(kIOPMSettingProModeDefer),
1579 		gIOPMSettingSilentRunningKey,
1580 		gIOPMSettingLowLatencyAudioModeKey,
1581 	};
1582 
1583 	OSSharedPtr<const OSSymbol> noPublishSettingsArr[kRootDomainNoPublishSettingsCount] =
1584 	{
1585 		OSSymbol::withCString(kIOPMSettingProModeControl),
1586 		OSSymbol::withCString(kIOPMSettingProModeDefer),
1587 		gIOPMSettingSilentRunningKey,
1588 		gIOPMSettingLowLatencyAudioModeKey,
1589 	};
1590 
1591 #if DEVELOPMENT || DEBUG
1592 #if defined(XNU_TARGET_OS_OSX)
1593 	PE_parse_boot_argn("darkwake", &gDarkWakeFlags, sizeof(gDarkWakeFlags));
1594 	PE_parse_boot_argn("clamshell", &gClamshellFlags, sizeof(gClamshellFlags));
1595 #endif /* defined(XNU_TARGET_OS_OSX) */
1596 #endif /* DEVELOPMENT || DEBUG */
1597 
1598 	PE_parse_boot_argn("noidle", &gNoIdleFlag, sizeof(gNoIdleFlag));
1599 	PE_parse_boot_argn("swd_sleeptimeout", &gSwdSleepTimeout, sizeof(gSwdSleepTimeout));
1600 	PE_parse_boot_argn("swd_waketimeout", &gSwdWakeTimeout, sizeof(gSwdWakeTimeout));
1601 	PE_parse_boot_argn("swd_timeout", &gSwdSleepWakeTimeout, sizeof(gSwdSleepWakeTimeout));
1602 	PE_parse_boot_argn("haltmspanic", &gHaltTimeMaxPanic, sizeof(gHaltTimeMaxPanic));
1603 	PE_parse_boot_argn("haltmslog", &gHaltTimeMaxLog, sizeof(gHaltTimeMaxLog));
1604 
1605 	// read noidle setting from Device Tree
1606 	if (PE_get_default("no-idle", &gNoIdleFlag, sizeof(gNoIdleFlag))) {
1607 		DLOG("Setting gNoIdleFlag to %u from device tree\n", gNoIdleFlag);
1608 	}
1609 
1610 	queue_init(&aggressivesQueue);
1611 	aggressivesThreadCall = thread_call_allocate(handleAggressivesFunction, this);
1612 	aggressivesData = OSData::withCapacity(
1613 		sizeof(AggressivesRecord) * (kPMLastAggressivenessType + 4));
1614 
1615 	featuresDictLock = IOLockAlloc();
1616 	settingsCtrlLock = IOLockAlloc();
1617 	wakeEventLock = IOLockAlloc();
1618 	gHaltLogLock = IOLockAlloc();
1619 	setPMRootDomain(this);
1620 
1621 	extraSleepTimer = thread_call_allocate(
1622 		idleSleepTimerExpired,
1623 		(thread_call_param_t) this);
1624 
1625 	powerButtonDown = thread_call_allocate(
1626 		powerButtonDownCallout,
1627 		(thread_call_param_t) this);
1628 
1629 	powerButtonUp = thread_call_allocate(
1630 		powerButtonUpCallout,
1631 		(thread_call_param_t) this);
1632 
1633 	diskSyncCalloutEntry = thread_call_allocate(
1634 		&disk_sync_callout,
1635 		(thread_call_param_t) this);
1636 	updateConsoleUsersEntry = thread_call_allocate(
1637 		&updateConsoleUsersCallout,
1638 		(thread_call_param_t) this);
1639 
1640 #if DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY
1641 	fullWakeThreadCall = thread_call_allocate_with_options(
1642 		OSMemberFunctionCast(thread_call_func_t, this,
1643 		&IOPMrootDomain::fullWakeDelayedWork),
1644 		(thread_call_param_t) this, THREAD_CALL_PRIORITY_KERNEL,
1645 		THREAD_CALL_OPTIONS_ONCE);
1646 #endif
1647 
1648 	setProperty(kIOSleepSupportedKey, true);
1649 
1650 	bzero(&gPMStats, sizeof(gPMStats));
1651 
1652 	pmTracer = PMTraceWorker::tracer(this);
1653 
1654 	pmAssertions = PMAssertionsTracker::pmAssertionsTracker(this);
1655 
1656 	userDisabledAllSleep = false;
1657 	systemBooting = true;
1658 	idleSleepEnabled = false;
1659 	idleSleepRevertible = true;
1660 	sleepSlider = 0;
1661 	idleSleepTimerPending = false;
1662 	wrangler = NULL;
1663 	clamshellClosed = false;
1664 	clamshellExists = false;
1665 #if DISPLAY_WRANGLER_PRESENT
1666 	clamshellDisabled = true;
1667 #else
1668 	clamshellDisabled = false;
1669 #endif
1670 	clamshellIgnoreClose = false;
1671 	acAdaptorConnected = true;
1672 	clamshellSleepDisableMask = 0;
1673 	gWakeReasonString[0] = '\0';
1674 
1675 	// Initialize to user active.
1676 	// Will never transition to user inactive w/o wrangler.
1677 	fullWakeReason = kFullWakeReasonLocalUser;
1678 	userIsActive = userWasActive = true;
1679 	clock_get_uptime(&gUserActiveAbsTime);
1680 	setProperty(gIOPMUserIsActiveKey.get(), kOSBooleanTrue);
1681 
1682 	// Set the default system capabilities at boot.
1683 	_currentCapability = kIOPMSystemCapabilityCPU      |
1684 	    kIOPMSystemCapabilityGraphics |
1685 	    kIOPMSystemCapabilityAudio    |
1686 	    kIOPMSystemCapabilityNetwork;
1687 
1688 	_pendingCapability = _currentCapability;
1689 	_desiredCapability = _currentCapability;
1690 	_highestCapability = _currentCapability;
1691 	setProperty(kIOPMSystemCapabilitiesKey, _currentCapability, 64);
1692 
1693 	queuedSleepWakeUUIDString = NULL;
1694 	initializeBootSessionUUID();
1695 	pmStatsAppResponses     = OSArray::withCapacity(5);
1696 	_statsNameKey           = OSSymbol::withCString(kIOPMStatsNameKey);
1697 	_statsPIDKey            = OSSymbol::withCString(kIOPMStatsPIDKey);
1698 	_statsTimeMSKey         = OSSymbol::withCString(kIOPMStatsTimeMSKey);
1699 	_statsResponseTypeKey   = OSSymbol::withCString(kIOPMStatsApplicationResponseTypeKey);
1700 	_statsMessageTypeKey    = OSSymbol::withCString(kIOPMStatsMessageTypeKey);
1701 	_statsPowerCapsKey      = OSSymbol::withCString(kIOPMStatsPowerCapabilityKey);
1702 	assertOnWakeSecs        = -1;// Invalid value to prevent updates
1703 
1704 	pmStatsLock = IOLockAlloc();
1705 	idxPMCPUClamshell = kCPUUnknownIndex;
1706 	idxPMCPULimitedPower = kCPUUnknownIndex;
1707 
1708 	tmpDict = OSDictionary::withCapacity(1);
1709 	setProperty(kRootDomainSupportedFeatures, tmpDict.get());
1710 
1711 	// Set a default "SystemPowerProfileOverrideDict" for platform
1712 	// drivers without any overrides.
1713 	if (!propertyExists(kIOPMSystemDefaultOverrideKey)) {
1714 		tmpDict = OSDictionary::withCapacity(1);
1715 		setProperty(kIOPMSystemDefaultOverrideKey, tmpDict.get());
1716 	}
1717 
1718 	settingsCallbacks = OSDictionary::withCapacity(1);
1719 
1720 	// Create a list of the valid PM settings that we'll relay to
1721 	// interested clients in setProperties() => setPMSetting()
1722 	allowedPMSettings = OSArray::withObjects(
1723 		(const OSObject **)settingsArr,
1724 		kRootDomainSettingsCount,
1725 		0);
1726 
1727 	// List of PM settings that should not automatically publish itself
1728 	// as a feature when registered by a listener.
1729 	noPublishPMSettings = OSArray::withObjects(
1730 		(const OSObject **)noPublishSettingsArr,
1731 		kRootDomainNoPublishSettingsCount,
1732 		0);
1733 
1734 	fPMSettingsDict = OSDictionary::withCapacity(5);
1735 	preventIdleSleepList = OSSet::withCapacity(8);
1736 	preventSystemSleepList = OSSet::withCapacity(2);
1737 
1738 	PMinit(); // creates gIOPMWorkLoop
1739 	gIOPMWorkLoop = getIOPMWorkloop();
1740 
1741 	// Create IOPMPowerStateQueue used to queue external power
1742 	// events, and to handle those events on the PM work loop.
1743 	pmPowerStateQueue = IOPMPowerStateQueue::PMPowerStateQueue(
1744 		this, OSMemberFunctionCast(IOEventSource::Action, this,
1745 		&IOPMrootDomain::dispatchPowerEvent));
1746 	gIOPMWorkLoop->addEventSource(pmPowerStateQueue);
1747 
1748 	_aotMode = 0;
1749 	_aotTimerES = IOTimerEventSource::timerEventSource(this,
1750 	    OSMemberFunctionCast(IOTimerEventSource::Action,
1751 	    this, &IOPMrootDomain::aotEvaluate));
1752 	gIOPMWorkLoop->addEventSource(_aotTimerES.get());
1753 
1754 	// Avoid publishing service early so gIOPMWorkLoop is
1755 	// guaranteed to be initialized by rootDomain.
1756 	publishPMRootDomain();
1757 
1758 	// create our power parent
1759 	gPatriarch = new IORootParent;
1760 	gPatriarch->init();
1761 	gPatriarch->attach(this);
1762 	gPatriarch->start(this);
1763 	gPatriarch->addPowerChild(this);
1764 
1765 	registerPowerDriver(this, ourPowerStates, NUM_POWER_STATES);
1766 	changePowerStateWithTagToPriv(ON_STATE, kCPSReasonInit);
1767 
1768 	// install power change handler
1769 	gSysPowerDownNotifier = registerPrioritySleepWakeInterest( &sysPowerDownHandler, this, NULL);
1770 
1771 #if DISPLAY_WRANGLER_PRESENT
1772 	wranglerIdleSettings = OSDictionary::withCapacity(1);
1773 	OSSharedPtr<OSNumber> wranglerIdlePeriod = OSNumber::withNumber(kDefaultWranglerIdlePeriod, 32);
1774 
1775 	if (wranglerIdleSettings && wranglerIdlePeriod) {
1776 		wranglerIdleSettings->setObject(kIORequestWranglerIdleKey,
1777 		    wranglerIdlePeriod.get());
1778 	}
1779 
1780 #endif /* DISPLAY_WRANGLER_PRESENT */
1781 
1782 	lowLatencyAudioNotifierDict       = OSDictionary::withCapacity(2);
1783 	lowLatencyAudioNotifyStateSym     = OSSymbol::withCString("LowLatencyAudioNotifyState");
1784 	lowLatencyAudioNotifyTimestampSym = OSSymbol::withCString("LowLatencyAudioNotifyTimestamp");
1785 	lowLatencyAudioNotifyStateVal     = OSNumber::withNumber(0ull, 32);
1786 	lowLatencyAudioNotifyTimestampVal = OSNumber::withNumber(0ull, 64);
1787 
1788 	if (lowLatencyAudioNotifierDict && lowLatencyAudioNotifyStateSym && lowLatencyAudioNotifyTimestampSym &&
1789 	    lowLatencyAudioNotifyStateVal && lowLatencyAudioNotifyTimestampVal) {
1790 		lowLatencyAudioNotifierDict->setObject(lowLatencyAudioNotifyStateSym.get(), lowLatencyAudioNotifyStateVal.get());
1791 		lowLatencyAudioNotifierDict->setObject(lowLatencyAudioNotifyTimestampSym.get(), lowLatencyAudioNotifyTimestampVal.get());
1792 	}
1793 
1794 	OSSharedPtr<const OSSymbol> ucClassName = OSSymbol::withCStringNoCopy("RootDomainUserClient");
1795 	setProperty(gIOUserClientClassKey, const_cast<OSObject *>(static_cast<const OSObject *>(ucClassName.get())));
1796 
1797 	// IOBacklightDisplay can take a long time to load at boot, or it may
1798 	// not load at all if you're booting with clamshell closed. We publish
1799 	// 'DisplayDims' here redundantly to get it published early and at all.
1800 	OSSharedPtr<OSDictionary> matching;
1801 	matching = serviceMatching("IOPMPowerSource");
1802 	psIterator = getMatchingServices(matching.get());
1803 
1804 	if (psIterator && psIterator->getNextObject()) {
1805 		// There's at least one battery on the system, so we publish
1806 		// 'DisplayDims' support for the LCD.
1807 		publishFeature("DisplayDims");
1808 	}
1809 
1810 	// read swd_panic boot-arg
1811 	PE_parse_boot_argn("swd_panic", &gSwdPanic, sizeof(gSwdPanic));
1812 	gWillShutdownSysctlRegistered = true;
1813 
1814 #if HIBERNATION
1815 	IOHibernateSystemInit(this);
1816 #endif
1817 
1818 	registerService();                  // let clients find us
1819 
1820 	return true;
1821 }
1822 
1823 //******************************************************************************
1824 // setProperties
1825 //
1826 // Receive a setProperty call
1827 // The "System Boot" property means the system is completely booted.
1828 //******************************************************************************
1829 
1830 IOReturn
setProperties(OSObject * props_obj)1831 IOPMrootDomain::setProperties( OSObject * props_obj )
1832 {
1833 	IOReturn        return_value = kIOReturnSuccess;
1834 	OSDictionary    *dict = OSDynamicCast(OSDictionary, props_obj);
1835 	OSBoolean       *b = NULL;
1836 	OSNumber        *n = NULL;
1837 	const OSSymbol  *key = NULL;
1838 	OSObject        *obj = NULL;
1839 	OSSharedPtr<OSCollectionIterator> iter;
1840 
1841 	if (!dict) {
1842 		return kIOReturnBadArgument;
1843 	}
1844 
1845 	bool clientEntitled = false;
1846 	{
1847 		OSSharedPtr<OSObject> obj = IOUserClient::copyClientEntitlement(current_task(), kRootDomainEntitlementSetProperty);
1848 		clientEntitled = (obj == kOSBooleanTrue);
1849 	}
1850 
1851 	if (!clientEntitled) {
1852 		const char * errorSuffix = NULL;
1853 
1854 		// IOPMSchedulePowerEvent() clients may not be entitled, but must be root.
1855 		// That API can set 6 possible keys that are checked below.
1856 		if ((dict->getCount() == 1) &&
1857 		    (dict->getObject(gIOPMSettingAutoWakeSecondsKey.get()) ||
1858 		    dict->getObject(gIOPMSettingAutoPowerSecondsKey.get()) ||
1859 		    dict->getObject(gIOPMSettingAutoWakeCalendarKey.get()) ||
1860 		    dict->getObject(gIOPMSettingAutoPowerCalendarKey.get()) ||
1861 		    dict->getObject(gIOPMSettingDebugWakeRelativeKey.get()) ||
1862 		    dict->getObject(gIOPMSettingDebugPowerRelativeKey.get()))) {
1863 			return_value = IOUserClient::clientHasPrivilege(current_task(), kIOClientPrivilegeAdministrator);
1864 			if (return_value != kIOReturnSuccess) {
1865 				errorSuffix = "privileged";
1866 			}
1867 		} else {
1868 			return_value = kIOReturnNotPermitted;
1869 			errorSuffix = "entitled";
1870 		}
1871 
1872 		if (return_value != kIOReturnSuccess) {
1873 			OSSharedPtr<OSString> procName(IOCopyLogNameForPID(proc_selfpid()), OSNoRetain);
1874 			DLOG("%s failed, process %s is not %s\n", __func__,
1875 			    procName ? procName->getCStringNoCopy() : "", errorSuffix);
1876 			return return_value;
1877 		}
1878 	}
1879 
1880 	OSSharedPtr<const OSSymbol> publish_simulated_battery_string    = OSSymbol::withCString("SoftwareSimulatedBatteries");
1881 	OSSharedPtr<const OSSymbol> boot_complete_string                = OSSymbol::withCString("System Boot Complete");
1882 	OSSharedPtr<const OSSymbol> sys_shutdown_string                 = OSSymbol::withCString("System Shutdown");
1883 	OSSharedPtr<const OSSymbol> stall_halt_string                   = OSSymbol::withCString("StallSystemAtHalt");
1884 	OSSharedPtr<const OSSymbol> battery_warning_disabled_string     = OSSymbol::withCString("BatteryWarningsDisabled");
1885 	OSSharedPtr<const OSSymbol> idle_seconds_string                 = OSSymbol::withCString("System Idle Seconds");
1886 	OSSharedPtr<const OSSymbol> idle_milliseconds_string            = OSSymbol::withCString("System Idle Milliseconds");
1887 	OSSharedPtr<const OSSymbol> sleepdisabled_string                = OSSymbol::withCString("SleepDisabled");
1888 	OSSharedPtr<const OSSymbol> ondeck_sleepwake_uuid_string        = OSSymbol::withCString(kIOPMSleepWakeUUIDKey);
1889 	OSSharedPtr<const OSSymbol> loginwindow_progress_string         = OSSymbol::withCString(kIOPMLoginWindowProgressKey);
1890 	OSSharedPtr<const OSSymbol> coredisplay_progress_string         = OSSymbol::withCString(kIOPMCoreDisplayProgressKey);
1891 	OSSharedPtr<const OSSymbol> coregraphics_progress_string        = OSSymbol::withCString(kIOPMCoreGraphicsProgressKey);
1892 #if DEBUG || DEVELOPMENT
1893 	OSSharedPtr<const OSSymbol> clamshell_close_string              = OSSymbol::withCString("IOPMTestClamshellClose");
1894 	OSSharedPtr<const OSSymbol> clamshell_open_string               = OSSymbol::withCString("IOPMTestClamshellOpen");
1895 	OSSharedPtr<const OSSymbol> ac_detach_string                    = OSSymbol::withCString("IOPMTestACDetach");
1896 	OSSharedPtr<const OSSymbol> ac_attach_string                    = OSSymbol::withCString("IOPMTestACAttach");
1897 	OSSharedPtr<const OSSymbol> desktopmode_set_string              = OSSymbol::withCString("IOPMTestDesktopModeSet");
1898 	OSSharedPtr<const OSSymbol> desktopmode_remove_string           = OSSymbol::withCString("IOPMTestDesktopModeRemove");
1899 #endif
1900 
1901 #if HIBERNATION
1902 	OSSharedPtr<const OSSymbol> hibernatemode_string                = OSSymbol::withCString(kIOHibernateModeKey);
1903 	OSSharedPtr<const OSSymbol> hibernatefile_string                = OSSymbol::withCString(kIOHibernateFileKey);
1904 	OSSharedPtr<const OSSymbol> hibernatefilemin_string             = OSSymbol::withCString(kIOHibernateFileMinSizeKey);
1905 	OSSharedPtr<const OSSymbol> hibernatefilemax_string             = OSSymbol::withCString(kIOHibernateFileMaxSizeKey);
1906 	OSSharedPtr<const OSSymbol> hibernatefreeratio_string           = OSSymbol::withCString(kIOHibernateFreeRatioKey);
1907 	OSSharedPtr<const OSSymbol> hibernatefreetime_string            = OSSymbol::withCString(kIOHibernateFreeTimeKey);
1908 #endif
1909 
1910 	iter = OSCollectionIterator::withCollection(dict);
1911 	if (!iter) {
1912 		return_value = kIOReturnNoMemory;
1913 		goto exit;
1914 	}
1915 
1916 	while ((key = (const OSSymbol *) iter->getNextObject()) &&
1917 	    (obj = dict->getObject(key))) {
1918 		if (key->isEqualTo(publish_simulated_battery_string.get())) {
1919 			if (OSDynamicCast(OSBoolean, obj)) {
1920 				publishResource(key, kOSBooleanTrue);
1921 			}
1922 		} else if (key->isEqualTo(idle_seconds_string.get())) {
1923 			if ((n = OSDynamicCast(OSNumber, obj))) {
1924 				setProperty(key, n);
1925 				idleMilliSeconds = n->unsigned32BitValue() * 1000;
1926 			}
1927 		} else if (key->isEqualTo(idle_milliseconds_string.get())) {
1928 			if ((n = OSDynamicCast(OSNumber, obj))) {
1929 				setProperty(key, n);
1930 				idleMilliSeconds = n->unsigned32BitValue();
1931 			}
1932 		} else if (key->isEqualTo(boot_complete_string.get())) {
1933 			pmPowerStateQueue->submitPowerEvent(kPowerEventSystemBootCompleted);
1934 		} else if (key->isEqualTo(sys_shutdown_string.get())) {
1935 			if ((b = OSDynamicCast(OSBoolean, obj))) {
1936 				pmPowerStateQueue->submitPowerEvent(kPowerEventSystemShutdown, (void *) b);
1937 			}
1938 		} else if (key->isEqualTo(battery_warning_disabled_string.get())) {
1939 			setProperty(key, obj);
1940 		}
1941 #if HIBERNATION
1942 		else if (key->isEqualTo(hibernatemode_string.get()) ||
1943 		    key->isEqualTo(hibernatefilemin_string.get()) ||
1944 		    key->isEqualTo(hibernatefilemax_string.get()) ||
1945 		    key->isEqualTo(hibernatefreeratio_string.get()) ||
1946 		    key->isEqualTo(hibernatefreetime_string.get())) {
1947 			if ((n = OSDynamicCast(OSNumber, obj))) {
1948 				setProperty(key, n);
1949 			}
1950 		} else if (key->isEqualTo(hibernatefile_string.get())) {
1951 			OSString * str = OSDynamicCast(OSString, obj);
1952 			if (str) {
1953 				setProperty(key, str);
1954 			}
1955 		}
1956 #endif
1957 		else if (key->isEqualTo(sleepdisabled_string.get())) {
1958 			if ((b = OSDynamicCast(OSBoolean, obj))) {
1959 				setProperty(key, b);
1960 				pmPowerStateQueue->submitPowerEvent(kPowerEventUserDisabledSleep, (void *) b);
1961 			}
1962 		} else if (key->isEqualTo(ondeck_sleepwake_uuid_string.get())) {
1963 			obj->retain();
1964 			pmPowerStateQueue->submitPowerEvent(kPowerEventQueueSleepWakeUUID, (void *)obj);
1965 		} else if (key->isEqualTo(loginwindow_progress_string.get())) {
1966 			if (pmTracer && (n = OSDynamicCast(OSNumber, obj))) {
1967 				uint32_t data = n->unsigned32BitValue();
1968 				pmTracer->traceComponentWakeProgress(kIOPMLoginWindowProgress, data);
1969 				kdebugTrace(kPMLogComponentWakeProgress, 0, kIOPMLoginWindowProgress, data);
1970 			}
1971 		} else if (key->isEqualTo(coredisplay_progress_string.get())) {
1972 			if (pmTracer && (n = OSDynamicCast(OSNumber, obj))) {
1973 				uint32_t data = n->unsigned32BitValue();
1974 				pmTracer->traceComponentWakeProgress(kIOPMCoreDisplayProgress, data);
1975 				kdebugTrace(kPMLogComponentWakeProgress, 0, kIOPMCoreDisplayProgress, data);
1976 			}
1977 		} else if (key->isEqualTo(coregraphics_progress_string.get())) {
1978 			if (pmTracer && (n = OSDynamicCast(OSNumber, obj))) {
1979 				uint32_t data = n->unsigned32BitValue();
1980 				pmTracer->traceComponentWakeProgress(kIOPMCoreGraphicsProgress, data);
1981 				kdebugTrace(kPMLogComponentWakeProgress, 0, kIOPMCoreGraphicsProgress, data);
1982 			}
1983 		} else if (key->isEqualTo(kIOPMDeepSleepEnabledKey) ||
1984 		    key->isEqualTo(kIOPMDestroyFVKeyOnStandbyKey) ||
1985 		    key->isEqualTo(kIOPMAutoPowerOffEnabledKey) ||
1986 		    key->isEqualTo(stall_halt_string.get())) {
1987 			if ((b = OSDynamicCast(OSBoolean, obj))) {
1988 				setProperty(key, b);
1989 			}
1990 		} else if (key->isEqualTo(kIOPMDeepSleepDelayKey) ||
1991 		    key->isEqualTo(kIOPMDeepSleepTimerKey) ||
1992 		    key->isEqualTo(kIOPMAutoPowerOffDelayKey) ||
1993 		    key->isEqualTo(kIOPMAutoPowerOffTimerKey)) {
1994 			if ((n = OSDynamicCast(OSNumber, obj))) {
1995 				setProperty(key, n);
1996 			}
1997 		} else if (key->isEqualTo(kIOPMUserWakeAlarmScheduledKey)) {
1998 			if (kOSBooleanTrue == obj) {
1999 				OSBitOrAtomic(kIOPMAlarmBitCalendarWake, &_userScheduledAlarmMask);
2000 			} else {
2001 				OSBitAndAtomic(~kIOPMAlarmBitCalendarWake, &_userScheduledAlarmMask);
2002 			}
2003 			DLOG("_userScheduledAlarmMask 0x%x\n", (uint32_t) _userScheduledAlarmMask);
2004 		}
2005 #if DEBUG || DEVELOPMENT
2006 		else if (key->isEqualTo(clamshell_close_string.get())) {
2007 			DLOG("SetProperties: setting clamshell close\n");
2008 			UInt32 msg = kIOPMClamshellClosed;
2009 			pmPowerStateQueue->submitPowerEvent(kPowerEventReceivedPowerNotification, (void *)(uintptr_t)msg);
2010 		} else if (key->isEqualTo(clamshell_open_string.get())) {
2011 			DLOG("SetProperties: setting clamshell open\n");
2012 			UInt32 msg = kIOPMClamshellOpened;
2013 			pmPowerStateQueue->submitPowerEvent(kPowerEventReceivedPowerNotification, (void *)(uintptr_t)msg);
2014 		} else if (key->isEqualTo(ac_detach_string.get())) {
2015 			DLOG("SetProperties: setting ac detach\n");
2016 			UInt32 msg = kIOPMSetACAdaptorConnected;
2017 			pmPowerStateQueue->submitPowerEvent(kPowerEventReceivedPowerNotification, (void *)(uintptr_t)msg);
2018 		} else if (key->isEqualTo(ac_attach_string.get())) {
2019 			DLOG("SetProperties: setting ac attach\n");
2020 			UInt32 msg = kIOPMSetACAdaptorConnected | kIOPMSetValue;
2021 			pmPowerStateQueue->submitPowerEvent(kPowerEventReceivedPowerNotification, (void *)(uintptr_t)msg);
2022 		} else if (key->isEqualTo(desktopmode_set_string.get())) {
2023 			DLOG("SetProperties: setting desktopmode");
2024 			UInt32 msg = kIOPMSetDesktopMode | kIOPMSetValue;
2025 			pmPowerStateQueue->submitPowerEvent(kPowerEventReceivedPowerNotification, (void *)(uintptr_t)msg);
2026 		} else if (key->isEqualTo(desktopmode_remove_string.get())) {
2027 			DLOG("SetProperties: removing desktopmode\n");
2028 			UInt32 msg = kIOPMSetDesktopMode;
2029 			pmPowerStateQueue->submitPowerEvent(kPowerEventReceivedPowerNotification, (void *)(uintptr_t)msg);
2030 		}
2031 #endif
2032 		// Relay our allowed PM settings onto our registered PM clients
2033 		else if ((allowedPMSettings->getNextIndexOfObject(key, 0) != (unsigned int) -1)) {
2034 			return_value = setPMSetting(key, obj);
2035 			if (kIOReturnSuccess != return_value) {
2036 				break;
2037 			}
2038 		} else {
2039 			DLOG("setProperties(%s) not handled\n", key->getCStringNoCopy());
2040 		}
2041 	}
2042 
2043 exit:
2044 	return return_value;
2045 }
2046 
2047 // MARK: -
2048 // MARK: Aggressiveness
2049 
2050 //******************************************************************************
2051 // setAggressiveness
2052 //
2053 // Override IOService::setAggressiveness()
2054 //******************************************************************************
2055 
2056 IOReturn
setAggressiveness(unsigned long type,unsigned long value)2057 IOPMrootDomain::setAggressiveness(
2058 	unsigned long   type,
2059 	unsigned long   value )
2060 {
2061 	return setAggressiveness( type, value, 0 );
2062 }
2063 
2064 /*
2065  * Private setAggressiveness() with an internal options argument.
2066  */
2067 IOReturn
setAggressiveness(unsigned long type,unsigned long value,IOOptionBits options)2068 IOPMrootDomain::setAggressiveness(
2069 	unsigned long   type,
2070 	unsigned long   value,
2071 	IOOptionBits    options )
2072 {
2073 	AggressivesRequest *    entry;
2074 	AggressivesRequest *    request;
2075 	bool                    found = false;
2076 
2077 	if ((type > UINT_MAX) || (value > UINT_MAX)) {
2078 		return kIOReturnBadArgument;
2079 	}
2080 
2081 	if (type == kPMMinutesToDim || type == kPMMinutesToSleep) {
2082 		DLOG("setAggressiveness(%x) %s = %u\n",
2083 		    (uint32_t) options, getAggressivenessTypeString((uint32_t) type), (uint32_t) value);
2084 	} else {
2085 		DEBUG_LOG("setAggressiveness(%x) %s = %u\n",
2086 		    (uint32_t) options, getAggressivenessTypeString((uint32_t) type), (uint32_t) value);
2087 	}
2088 
2089 	request = IOMallocType(AggressivesRequest);
2090 	request->options  = options;
2091 	request->dataType = kAggressivesRequestTypeRecord;
2092 	request->data.record.type  = (uint32_t) type;
2093 	request->data.record.value = (uint32_t) value;
2094 
2095 	AGGRESSIVES_LOCK();
2096 
2097 	// Update disk quick spindown flag used by getAggressiveness().
2098 	// Never merge requests with quick spindown flags set.
2099 
2100 	if (options & kAggressivesOptionQuickSpindownEnable) {
2101 		gAggressivesState |= kAggressivesStateQuickSpindown;
2102 	} else if (options & kAggressivesOptionQuickSpindownDisable) {
2103 		gAggressivesState &= ~kAggressivesStateQuickSpindown;
2104 	} else {
2105 		// Coalesce requests with identical aggressives types.
2106 		// Deal with callers that calls us too "aggressively".
2107 
2108 		queue_iterate(&aggressivesQueue, entry, AggressivesRequest *, chain)
2109 		{
2110 			if ((entry->dataType == kAggressivesRequestTypeRecord) &&
2111 			    (entry->data.record.type == type) &&
2112 			    ((entry->options & kAggressivesOptionQuickSpindownMask) == 0)) {
2113 				entry->data.record.value = (uint32_t) value;
2114 				found = true;
2115 				break;
2116 			}
2117 		}
2118 	}
2119 
2120 	if (!found) {
2121 		queue_enter(&aggressivesQueue, request, AggressivesRequest *, chain);
2122 	}
2123 
2124 	AGGRESSIVES_UNLOCK();
2125 
2126 	if (found) {
2127 		IOFreeType(request, AggressivesRequest);
2128 	}
2129 
2130 	if (options & kAggressivesOptionSynchronous) {
2131 		handleAggressivesRequests(); // not truly synchronous
2132 	} else {
2133 		thread_call_enter(aggressivesThreadCall);
2134 	}
2135 
2136 	return kIOReturnSuccess;
2137 }
2138 
2139 //******************************************************************************
2140 // getAggressiveness
2141 //
2142 // Override IOService::setAggressiveness()
2143 // Fetch the aggressiveness factor with the given type.
2144 //******************************************************************************
2145 
2146 IOReturn
getAggressiveness(unsigned long type,unsigned long * outLevel)2147 IOPMrootDomain::getAggressiveness(
2148 	unsigned long   type,
2149 	unsigned long * outLevel )
2150 {
2151 	uint32_t    value  = 0;
2152 	int         source = 0;
2153 
2154 	if (!outLevel || (type > UINT_MAX)) {
2155 		return kIOReturnBadArgument;
2156 	}
2157 
2158 	AGGRESSIVES_LOCK();
2159 
2160 	// Disk quick spindown in effect, report value = 1
2161 
2162 	if ((gAggressivesState & kAggressivesStateQuickSpindown) &&
2163 	    (type == kPMMinutesToSpinDown)) {
2164 		value  = kAggressivesMinValue;
2165 		source = 1;
2166 	}
2167 
2168 	// Consult the pending request queue.
2169 
2170 	if (!source) {
2171 		AggressivesRequest * entry;
2172 
2173 		queue_iterate(&aggressivesQueue, entry, AggressivesRequest *, chain)
2174 		{
2175 			if ((entry->dataType == kAggressivesRequestTypeRecord) &&
2176 			    (entry->data.record.type == type) &&
2177 			    ((entry->options & kAggressivesOptionQuickSpindownMask) == 0)) {
2178 				value  = entry->data.record.value;
2179 				source = 2;
2180 				break;
2181 			}
2182 		}
2183 	}
2184 
2185 	// Consult the backend records.
2186 
2187 	if (!source && aggressivesData) {
2188 		AggressivesRecord * record;
2189 		int                 i, count;
2190 
2191 		count  = aggressivesData->getLength() / sizeof(AggressivesRecord);
2192 		record = (AggressivesRecord *) aggressivesData->getBytesNoCopy();
2193 
2194 		for (i = 0; i < count; i++, record++) {
2195 			if (record->type == type) {
2196 				value  = record->value;
2197 				source = 3;
2198 				break;
2199 			}
2200 		}
2201 	}
2202 
2203 	AGGRESSIVES_UNLOCK();
2204 
2205 	if (source) {
2206 		*outLevel = (unsigned long) value;
2207 		return kIOReturnSuccess;
2208 	} else {
2209 		DLOG("getAggressiveness type 0x%x not found\n", (uint32_t) type);
2210 		*outLevel = 0; // default return = 0, driver may not check for error
2211 		return kIOReturnInvalid;
2212 	}
2213 }
2214 
2215 //******************************************************************************
2216 // joinAggressiveness
2217 //
2218 // Request from IOService to join future aggressiveness broadcasts.
2219 //******************************************************************************
2220 
2221 IOReturn
joinAggressiveness(IOService * service)2222 IOPMrootDomain::joinAggressiveness(
2223 	IOService * service )
2224 {
2225 	AggressivesRequest *    request;
2226 
2227 	if (!service || (service == this)) {
2228 		return kIOReturnBadArgument;
2229 	}
2230 
2231 	DEBUG_LOG("joinAggressiveness %s %p\n", service->getName(), OBFUSCATE(service));
2232 
2233 	request = IOMallocType(AggressivesRequest);
2234 	request->dataType = kAggressivesRequestTypeService;
2235 	request->data.service.reset(service, OSRetain); // released by synchronizeAggressives()
2236 
2237 	AGGRESSIVES_LOCK();
2238 	queue_enter(&aggressivesQueue, request, AggressivesRequest *, chain);
2239 	AGGRESSIVES_UNLOCK();
2240 
2241 	thread_call_enter(aggressivesThreadCall);
2242 
2243 	return kIOReturnSuccess;
2244 }
2245 
2246 //******************************************************************************
2247 // handleAggressivesRequests
2248 //
2249 // Backend thread processes all incoming aggressiveness requests in the queue.
2250 //******************************************************************************
2251 
2252 static void
handleAggressivesFunction(thread_call_param_t param1,thread_call_param_t param2)2253 handleAggressivesFunction(
2254 	thread_call_param_t param1,
2255 	thread_call_param_t param2 )
2256 {
2257 	if (param1) {
2258 		((IOPMrootDomain *) param1)->handleAggressivesRequests();
2259 	}
2260 }
2261 
2262 void
handleAggressivesRequests(void)2263 IOPMrootDomain::handleAggressivesRequests( void )
2264 {
2265 	AggressivesRecord *     start;
2266 	AggressivesRecord *     record;
2267 	AggressivesRequest *    request;
2268 	queue_head_t            joinedQueue;
2269 	int                     i, count;
2270 	bool                    broadcast;
2271 	bool                    found;
2272 	bool                    pingSelf = false;
2273 
2274 	AGGRESSIVES_LOCK();
2275 
2276 	if ((gAggressivesState & kAggressivesStateBusy) || !aggressivesData ||
2277 	    queue_empty(&aggressivesQueue)) {
2278 		goto unlock_done;
2279 	}
2280 
2281 	gAggressivesState |= kAggressivesStateBusy;
2282 	count = aggressivesData->getLength() / sizeof(AggressivesRecord);
2283 	start = (AggressivesRecord *) aggressivesData->getBytesNoCopy();
2284 
2285 	do{
2286 		broadcast = false;
2287 		queue_init(&joinedQueue);
2288 
2289 		do{
2290 			// Remove request from the incoming queue in FIFO order.
2291 			queue_remove_first(&aggressivesQueue, request, AggressivesRequest *, chain);
2292 			switch (request->dataType) {
2293 			case kAggressivesRequestTypeRecord:
2294 				// Update existing record if found.
2295 				found = false;
2296 				for (i = 0, record = start; i < count; i++, record++) {
2297 					if (record->type == request->data.record.type) {
2298 						found = true;
2299 
2300 						if (request->options & kAggressivesOptionQuickSpindownEnable) {
2301 							if ((record->flags & kAggressivesRecordFlagMinValue) == 0) {
2302 								broadcast = true;
2303 								record->flags |= (kAggressivesRecordFlagMinValue |
2304 								    kAggressivesRecordFlagModified);
2305 								DLOG("disk spindown accelerated, was %u min\n",
2306 								    record->value);
2307 							}
2308 						} else if (request->options & kAggressivesOptionQuickSpindownDisable) {
2309 							if (record->flags & kAggressivesRecordFlagMinValue) {
2310 								broadcast = true;
2311 								record->flags |= kAggressivesRecordFlagModified;
2312 								record->flags &= ~kAggressivesRecordFlagMinValue;
2313 								DLOG("disk spindown restored to %u min\n",
2314 								    record->value);
2315 							}
2316 						} else if (record->value != request->data.record.value) {
2317 							record->value = request->data.record.value;
2318 							if ((record->flags & kAggressivesRecordFlagMinValue) == 0) {
2319 								broadcast = true;
2320 								record->flags |= kAggressivesRecordFlagModified;
2321 							}
2322 						}
2323 						break;
2324 					}
2325 				}
2326 
2327 				// No matching record, append a new record.
2328 				if (!found &&
2329 				    ((request->options & kAggressivesOptionQuickSpindownDisable) == 0)) {
2330 					AggressivesRecord   newRecord;
2331 
2332 					newRecord.flags = kAggressivesRecordFlagModified;
2333 					newRecord.type  = request->data.record.type;
2334 					newRecord.value = request->data.record.value;
2335 					if (request->options & kAggressivesOptionQuickSpindownEnable) {
2336 						newRecord.flags |= kAggressivesRecordFlagMinValue;
2337 						DLOG("disk spindown accelerated\n");
2338 					}
2339 
2340 					aggressivesData->appendValue(newRecord);
2341 
2342 					// OSData may have switched to another (larger) buffer.
2343 					count = aggressivesData->getLength() / sizeof(AggressivesRecord);
2344 					start = (AggressivesRecord *) aggressivesData->getBytesNoCopy();
2345 					broadcast = true;
2346 				}
2347 
2348 				// Finished processing the request, release it.
2349 				IOFreeType(request, AggressivesRequest);
2350 				break;
2351 
2352 			case kAggressivesRequestTypeService:
2353 				// synchronizeAggressives() will free request.
2354 				queue_enter(&joinedQueue, request, AggressivesRequest *, chain);
2355 				break;
2356 
2357 			default:
2358 				panic("bad aggressives request type %x", request->dataType);
2359 				break;
2360 			}
2361 		} while (!queue_empty(&aggressivesQueue));
2362 
2363 		// Release the lock to perform work, with busy flag set.
2364 		if (!queue_empty(&joinedQueue) || broadcast) {
2365 			AGGRESSIVES_UNLOCK();
2366 			if (!queue_empty(&joinedQueue)) {
2367 				synchronizeAggressives(&joinedQueue, start, count);
2368 			}
2369 			if (broadcast) {
2370 				broadcastAggressives(start, count);
2371 			}
2372 			AGGRESSIVES_LOCK();
2373 		}
2374 
2375 		// Remove the modified flag from all records.
2376 		for (i = 0, record = start; i < count; i++, record++) {
2377 			if ((record->flags & kAggressivesRecordFlagModified) &&
2378 			    ((record->type == kPMMinutesToDim) ||
2379 			    (record->type == kPMMinutesToSleep))) {
2380 				pingSelf = true;
2381 			}
2382 
2383 			record->flags &= ~kAggressivesRecordFlagModified;
2384 		}
2385 
2386 		// Check the incoming queue again since new entries may have been
2387 		// added while lock was released above.
2388 	} while (!queue_empty(&aggressivesQueue));
2389 
2390 	gAggressivesState &= ~kAggressivesStateBusy;
2391 
2392 unlock_done:
2393 	AGGRESSIVES_UNLOCK();
2394 
2395 	// Root domain is interested in system and display sleep slider changes.
2396 	// Submit a power event to handle those changes on the PM work loop.
2397 
2398 	if (pingSelf && pmPowerStateQueue) {
2399 		pmPowerStateQueue->submitPowerEvent(
2400 			kPowerEventPolicyStimulus,
2401 			(void *) kStimulusAggressivenessChanged );
2402 	}
2403 }
2404 
2405 //******************************************************************************
2406 // synchronizeAggressives
2407 //
2408 // Push all known aggressiveness records to one or more IOService.
2409 //******************************************************************************
2410 
2411 void
synchronizeAggressives(queue_head_t * joinedQueue,const AggressivesRecord * array,int count)2412 IOPMrootDomain::synchronizeAggressives(
2413 	queue_head_t *              joinedQueue,
2414 	const AggressivesRecord *   array,
2415 	int                         count )
2416 {
2417 	OSSharedPtr<IOService>      service;
2418 	AggressivesRequest *        request;
2419 	const AggressivesRecord *   record;
2420 	IOPMDriverCallEntry         callEntry;
2421 	uint32_t                    value;
2422 	int                         i;
2423 
2424 	while (!queue_empty(joinedQueue)) {
2425 		queue_remove_first(joinedQueue, request, AggressivesRequest *, chain);
2426 		if (request->dataType == kAggressivesRequestTypeService) {
2427 			// retained by joinAggressiveness(), so take ownership
2428 			service = os::move(request->data.service);
2429 		} else {
2430 			service.reset();
2431 		}
2432 
2433 		IOFreeType(request, AggressivesRequest);
2434 		request = NULL;
2435 
2436 		if (service) {
2437 			if (service->assertPMDriverCall(&callEntry, kIOPMDriverCallMethodSetAggressive)) {
2438 				for (i = 0, record = array; i < count; i++, record++) {
2439 					value = record->value;
2440 					if (record->flags & kAggressivesRecordFlagMinValue) {
2441 						value = kAggressivesMinValue;
2442 					}
2443 
2444 					_LOG("synchronizeAggressives 0x%x = %u to %s\n",
2445 					    record->type, value, service->getName());
2446 					service->setAggressiveness(record->type, value);
2447 				}
2448 				service->deassertPMDriverCall(&callEntry);
2449 			}
2450 		}
2451 	}
2452 }
2453 
2454 //******************************************************************************
2455 // broadcastAggressives
2456 //
2457 // Traverse PM tree and call setAggressiveness() for records that have changed.
2458 //******************************************************************************
2459 
2460 void
broadcastAggressives(const AggressivesRecord * array,int count)2461 IOPMrootDomain::broadcastAggressives(
2462 	const AggressivesRecord *   array,
2463 	int                         count )
2464 {
2465 	OSSharedPtr<IORegistryIterator> iter;
2466 	IORegistryEntry                *entry;
2467 	OSSharedPtr<IORegistryEntry>    child;
2468 	IOPowerConnection              *connect;
2469 	IOService                      *service;
2470 	const AggressivesRecord        *record;
2471 	IOPMDriverCallEntry             callEntry;
2472 	uint32_t                        value;
2473 	int                             i;
2474 
2475 	iter = IORegistryIterator::iterateOver(
2476 		this, gIOPowerPlane, kIORegistryIterateRecursively);
2477 	if (iter) {
2478 		do{
2479 			// !! reset the iterator
2480 			iter->reset();
2481 			while ((entry = iter->getNextObject())) {
2482 				connect = OSDynamicCast(IOPowerConnection, entry);
2483 				if (!connect || !connect->getReadyFlag()) {
2484 					continue;
2485 				}
2486 
2487 				child = connect->copyChildEntry(gIOPowerPlane);
2488 				if (child) {
2489 					if ((service = OSDynamicCast(IOService, child.get()))) {
2490 						if (service->assertPMDriverCall(&callEntry, kIOPMDriverCallMethodSetAggressive)) {
2491 							for (i = 0, record = array; i < count; i++, record++) {
2492 								if (record->flags & kAggressivesRecordFlagModified) {
2493 									value = record->value;
2494 									if (record->flags & kAggressivesRecordFlagMinValue) {
2495 										value = kAggressivesMinValue;
2496 									}
2497 									_LOG("broadcastAggressives %x = %u to %s\n",
2498 									    record->type, value, service->getName());
2499 									service->setAggressiveness(record->type, value);
2500 								}
2501 							}
2502 							service->deassertPMDriverCall(&callEntry);
2503 						}
2504 					}
2505 				}
2506 			}
2507 		}while (!entry && !iter->isValid());
2508 	}
2509 }
2510 
2511 //*****************************************
2512 // stackshot on power button press
2513 // ***************************************
2514 static void
powerButtonDownCallout(thread_call_param_t us,thread_call_param_t)2515 powerButtonDownCallout(thread_call_param_t us, thread_call_param_t )
2516 {
2517 	/* Power button pressed during wake
2518 	 * Take a stackshot
2519 	 */
2520 	DEBUG_LOG("Powerbutton: down. Taking stackshot\n");
2521 	((IOPMrootDomain *)us)->takeStackshot(false);
2522 }
2523 
2524 static void
powerButtonUpCallout(thread_call_param_t us,thread_call_param_t)2525 powerButtonUpCallout(thread_call_param_t us, thread_call_param_t)
2526 {
2527 	/* Power button released.
2528 	 * Delete any stackshot data
2529 	 */
2530 	DEBUG_LOG("PowerButton: up callout. Delete stackshot\n");
2531 	((IOPMrootDomain *)us)->deleteStackshot();
2532 }
2533 //*************************************************************************
2534 //
2535 
2536 // MARK: -
2537 // MARK: System Sleep
2538 
2539 //******************************************************************************
2540 // startIdleSleepTimer
2541 //
2542 //******************************************************************************
2543 
2544 void
startIdleSleepTimer(uint32_t inMilliSeconds)2545 IOPMrootDomain::startIdleSleepTimer( uint32_t inMilliSeconds )
2546 {
2547 	AbsoluteTime deadline;
2548 
2549 	ASSERT_GATED();
2550 	if (gNoIdleFlag) {
2551 		DLOG("idle timer not set (noidle=%d)\n", gNoIdleFlag);
2552 		return;
2553 	}
2554 	if (inMilliSeconds) {
2555 		if (inMilliSeconds < kMinimumTimeBeforeIdleSleep) {
2556 			AbsoluteTime    now;
2557 			uint64_t        nsec_since_wake;
2558 			uint64_t                msec_since_wake;
2559 
2560 			// Adjust idle timer so it will not expire until atleast kMinimumTimeBeforeIdleSleep milliseconds
2561 			// after the most recent AP wake.
2562 			clock_get_uptime(&now);
2563 			SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
2564 			absolutetime_to_nanoseconds(now, &nsec_since_wake);
2565 			msec_since_wake = nsec_since_wake / NSEC_PER_MSEC;
2566 
2567 			if (msec_since_wake < kMinimumTimeBeforeIdleSleep) {
2568 				uint32_t newIdleTimer = kMinimumTimeBeforeIdleSleep - (uint32_t)msec_since_wake;
2569 
2570 				// Ensure that our new idle timer is not less than inMilliSeconds,
2571 				// as we should only be increasing the timer duration, not decreasing it
2572 				if (newIdleTimer > inMilliSeconds) {
2573 					DLOG("startIdleSleepTimer increasing timeout from %u to %u\n", inMilliSeconds, newIdleTimer);
2574 					inMilliSeconds = newIdleTimer;
2575 				}
2576 			}
2577 		}
2578 		clock_interval_to_deadline(inMilliSeconds, kMillisecondScale, &deadline);
2579 		thread_call_enter_delayed(extraSleepTimer, deadline);
2580 		idleSleepTimerPending = true;
2581 	} else {
2582 		thread_call_enter(extraSleepTimer);
2583 	}
2584 	DLOG("idle timer set for %u milliseconds\n", inMilliSeconds);
2585 }
2586 
2587 //******************************************************************************
2588 // cancelIdleSleepTimer
2589 //
2590 //******************************************************************************
2591 
2592 void
cancelIdleSleepTimer(void)2593 IOPMrootDomain::cancelIdleSleepTimer( void )
2594 {
2595 	ASSERT_GATED();
2596 	if (idleSleepTimerPending) {
2597 		DLOG("idle timer cancelled\n");
2598 		thread_call_cancel(extraSleepTimer);
2599 		idleSleepTimerPending = false;
2600 
2601 		if (!assertOnWakeSecs && gIOLastWakeAbsTime) {
2602 			AbsoluteTime    now;
2603 			clock_usec_t    microsecs;
2604 			clock_get_uptime(&now);
2605 			SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
2606 			absolutetime_to_microtime(now, &assertOnWakeSecs, &microsecs);
2607 			if (assertOnWakeReport) {
2608 				HISTREPORT_TALLYVALUE(assertOnWakeReport, (int64_t)assertOnWakeSecs);
2609 				DLOG("Updated assertOnWake %lu\n", (unsigned long)assertOnWakeSecs);
2610 			}
2611 		}
2612 	}
2613 }
2614 
2615 //******************************************************************************
2616 // idleSleepTimerExpired
2617 //
2618 //******************************************************************************
2619 
2620 static void
idleSleepTimerExpired(thread_call_param_t us,thread_call_param_t)2621 idleSleepTimerExpired(
2622 	thread_call_param_t us, thread_call_param_t )
2623 {
2624 	((IOPMrootDomain *)us)->handleSleepTimerExpiration();
2625 }
2626 
2627 //******************************************************************************
2628 // handleSleepTimerExpiration
2629 //
2630 // The time between the sleep idle timeout and the next longest one has elapsed.
2631 // It's time to sleep. Start that by removing the clamp that's holding us awake.
2632 //******************************************************************************
2633 
2634 void
handleSleepTimerExpiration(void)2635 IOPMrootDomain::handleSleepTimerExpiration( void )
2636 {
2637 	if (!gIOPMWorkLoop->inGate()) {
2638 		gIOPMWorkLoop->runAction(
2639 			OSMemberFunctionCast(IOWorkLoop::Action, this,
2640 			&IOPMrootDomain::handleSleepTimerExpiration),
2641 			this);
2642 		return;
2643 	}
2644 
2645 	DLOG("sleep timer expired\n");
2646 	ASSERT_GATED();
2647 
2648 	idleSleepTimerPending = false;
2649 	setQuickSpinDownTimeout();
2650 	adjustPowerState(true);
2651 }
2652 
2653 //******************************************************************************
2654 // getTimeToIdleSleep
2655 //
2656 // Returns number of milliseconds left before going into idle sleep.
2657 // Caller has to make sure that idle sleep is allowed at the time of calling
2658 // this function
2659 //******************************************************************************
2660 
2661 uint32_t
getTimeToIdleSleep(void)2662 IOPMrootDomain::getTimeToIdleSleep( void )
2663 {
2664 	AbsoluteTime    now, lastActivityTime;
2665 	uint64_t        nanos;
2666 	uint32_t        minutesSinceUserInactive = 0;
2667 	uint32_t        sleepDelay = 0;
2668 
2669 	if (!idleSleepEnabled) {
2670 		return 0xffffffff;
2671 	}
2672 
2673 	if (userActivityTime) {
2674 		lastActivityTime = userActivityTime;
2675 	} else {
2676 		lastActivityTime = userBecameInactiveTime;
2677 	}
2678 
2679 	// Ignore any lastActivityTime that predates the last system wake.
2680 	// The goal is to avoid a sudden idle sleep right after a dark wake
2681 	// due to sleepDelay=0 computed below. The alternative 60s minimum
2682 	// timeout should be large enough to allow dark wake to complete,
2683 	// at which point the idle timer will be promptly cancelled.
2684 	clock_get_uptime(&now);
2685 	if ((CMP_ABSOLUTETIME(&lastActivityTime, &gIOLastWakeAbsTime) >= 0) &&
2686 	    (CMP_ABSOLUTETIME(&now, &lastActivityTime) > 0)) {
2687 		SUB_ABSOLUTETIME(&now, &lastActivityTime);
2688 		absolutetime_to_nanoseconds(now, &nanos);
2689 		minutesSinceUserInactive = nanos / (60000000000ULL);
2690 
2691 		if (minutesSinceUserInactive >= sleepSlider) {
2692 			sleepDelay = 0;
2693 		} else {
2694 			sleepDelay = sleepSlider - minutesSinceUserInactive;
2695 		}
2696 	} else {
2697 		DLOG("ignoring lastActivityTime 0x%qx, now 0x%qx, wake 0x%qx\n",
2698 		    lastActivityTime, now, gIOLastWakeAbsTime);
2699 		sleepDelay = sleepSlider;
2700 	}
2701 
2702 	DLOG("user inactive %u min, time to idle sleep %u min\n",
2703 	    minutesSinceUserInactive, sleepDelay);
2704 
2705 	return sleepDelay * 60 * 1000;
2706 }
2707 
2708 //******************************************************************************
2709 // setQuickSpinDownTimeout
2710 //
2711 //******************************************************************************
2712 
2713 void
setQuickSpinDownTimeout(void)2714 IOPMrootDomain::setQuickSpinDownTimeout( void )
2715 {
2716 	ASSERT_GATED();
2717 	setAggressiveness(
2718 		kPMMinutesToSpinDown, 0, kAggressivesOptionQuickSpindownEnable );
2719 }
2720 
2721 //******************************************************************************
2722 // restoreUserSpinDownTimeout
2723 //
2724 //******************************************************************************
2725 
2726 void
restoreUserSpinDownTimeout(void)2727 IOPMrootDomain::restoreUserSpinDownTimeout( void )
2728 {
2729 	ASSERT_GATED();
2730 	setAggressiveness(
2731 		kPMMinutesToSpinDown, 0, kAggressivesOptionQuickSpindownDisable );
2732 }
2733 
2734 //******************************************************************************
2735 // sleepSystem
2736 //
2737 //******************************************************************************
2738 
2739 /* public */
2740 IOReturn
sleepSystem(void)2741 IOPMrootDomain::sleepSystem( void )
2742 {
2743 	return sleepSystemOptions(NULL);
2744 }
2745 
2746 /* private */
2747 IOReturn
sleepSystemOptions(OSDictionary * options)2748 IOPMrootDomain::sleepSystemOptions( OSDictionary *options )
2749 {
2750 	OSObject *obj = NULL;
2751 	OSString *reason = NULL;
2752 	/* sleepSystem is a public function, and may be called by any kernel driver.
2753 	 * And that's bad - drivers should sleep the system by calling
2754 	 * receivePowerNotification() instead. Drivers should not use sleepSystem.
2755 	 *
2756 	 * Note that user space app calls to IOPMSleepSystem() will also travel
2757 	 * this code path and thus be correctly identified as software sleeps.
2758 	 */
2759 
2760 	if (options && options->getObject("OSSwitch")) {
2761 		// Log specific sleep cause for OS Switch hibernation
2762 		return privateSleepSystem( kIOPMSleepReasonOSSwitchHibernate);
2763 	}
2764 
2765 	if (options && (obj = options->getObject("Sleep Reason"))) {
2766 		reason = OSDynamicCast(OSString, obj);
2767 		if (reason && reason->isEqualTo(kIOPMDarkWakeThermalEmergencyKey)) {
2768 			return privateSleepSystem(kIOPMSleepReasonDarkWakeThermalEmergency);
2769 		}
2770 		if (reason && reason->isEqualTo(kIOPMNotificationWakeExitKey)) {
2771 			return privateSleepSystem(kIOPMSleepReasonNotificationWakeExit);
2772 		}
2773 	}
2774 
2775 	return privateSleepSystem( kIOPMSleepReasonSoftware);
2776 }
2777 
2778 /* private */
2779 IOReturn
privateSleepSystem(uint32_t sleepReason)2780 IOPMrootDomain::privateSleepSystem( uint32_t sleepReason )
2781 {
2782 	/* Called from both gated and non-gated context */
2783 
2784 	if (!checkSystemSleepEnabled() || !pmPowerStateQueue) {
2785 		return kIOReturnNotPermitted;
2786 	}
2787 
2788 	pmPowerStateQueue->submitPowerEvent(
2789 		kPowerEventPolicyStimulus,
2790 		(void *) kStimulusDemandSystemSleep,
2791 		sleepReason);
2792 
2793 	return kIOReturnSuccess;
2794 }
2795 
2796 //******************************************************************************
2797 // powerChangeDone
2798 //
2799 // This overrides powerChangeDone in IOService.
2800 //******************************************************************************
2801 void
powerChangeDone(unsigned long previousPowerState)2802 IOPMrootDomain::powerChangeDone( unsigned long previousPowerState )
2803 {
2804 #if !__i386__ && !__x86_64__
2805 	uint64_t    timeSinceReset = 0;
2806 #endif
2807 	uint64_t           now;
2808 	unsigned long      newState;
2809 	clock_sec_t        secs;
2810 	clock_usec_t       microsecs;
2811 	uint32_t           lastDebugWakeSeconds;
2812 	clock_sec_t        adjWakeTime;
2813 	IOPMCalendarStruct nowCalendar;
2814 
2815 	ASSERT_GATED();
2816 	newState = getPowerState();
2817 	DLOG("PowerChangeDone: %s->%s\n",
2818 	    getPowerStateString((uint32_t) previousPowerState), getPowerStateString((uint32_t) getPowerState()));
2819 
2820 	if (previousPowerState == newState) {
2821 		return;
2822 	}
2823 
2824 	notifierThread = current_thread();
2825 	switch (getPowerState()) {
2826 	case SLEEP_STATE: {
2827 		if (kPMCalendarTypeInvalid != _aotWakeTimeCalendar.selector) {
2828 			secs = 0;
2829 			microsecs = 0;
2830 			PEGetUTCTimeOfDay(&secs, &microsecs);
2831 
2832 			adjWakeTime = 0;
2833 			if ((kIOPMAOTModeRespectTimers & _aotMode) && (_calendarWakeAlarmUTC < _aotWakeTimeUTC)) {
2834 				IOLog("use _calendarWakeAlarmUTC\n");
2835 				adjWakeTime = _calendarWakeAlarmUTC;
2836 			} else if (kIOPMWakeEventAOTExitFlags & _aotPendingFlags) {
2837 				IOLog("accelerate _aotWakeTime for exit\n");
2838 				adjWakeTime = secs;
2839 			} else if (kIOPMDriverAssertionLevelOn == getPMAssertionLevel(kIOPMDriverAssertionCPUBit)) {
2840 				IOLog("accelerate _aotWakeTime for assertion\n");
2841 				adjWakeTime = secs;
2842 			}
2843 			if (adjWakeTime) {
2844 				IOPMConvertSecondsToCalendar(adjWakeTime, &_aotWakeTimeCalendar);
2845 			}
2846 
2847 			IOPMConvertSecondsToCalendar(secs, &nowCalendar);
2848 			IOLog("aotSleep at " YMDTF " sched: " YMDTF "\n", YMDT(&nowCalendar), YMDT(&_aotWakeTimeCalendar));
2849 
2850 			IOReturn __unused ret = setMaintenanceWakeCalendar(&_aotWakeTimeCalendar);
2851 			assert(kIOReturnSuccess == ret);
2852 		}
2853 		if (_aotLastWakeTime) {
2854 			_aotMetrics->totalTime += mach_absolute_time() - _aotLastWakeTime;
2855 			if (_aotMetrics->sleepCount && (_aotMetrics->sleepCount <= kIOPMAOTMetricsKernelWakeCountMax)) {
2856 				strlcpy(&_aotMetrics->kernelWakeReason[_aotMetrics->sleepCount - 1][0],
2857 				    gWakeReasonString,
2858 				    sizeof(_aotMetrics->kernelWakeReason[_aotMetrics->sleepCount]));
2859 			}
2860 		}
2861 		_aotPendingFlags &= ~kIOPMWakeEventAOTPerCycleFlags;
2862 		if (_aotTimerScheduled) {
2863 			_aotTimerES->cancelTimeout();
2864 			_aotTimerScheduled = false;
2865 		}
2866 		acceptSystemWakeEvents(kAcceptSystemWakeEvents_Enable);
2867 
2868 		// re-enable this timer for next sleep
2869 		cancelIdleSleepTimer();
2870 
2871 		if (clamshellExists) {
2872 #if DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY
2873 			if (gClamshellFlags & kClamshell_WAR_58009435) {
2874 				// Disable clamshell sleep until system has completed full wake.
2875 				// This prevents a system sleep request (due to a clamshell close)
2876 				// from being queued until the end of system full wake - even if
2877 				// other clamshell disable bits outside of our control is wrong.
2878 				setClamShellSleepDisable(true, kClamshellSleepDisableInternal);
2879 			}
2880 #endif
2881 
2882 			// Log the last known clamshell state before system sleep
2883 			DLOG("clamshell closed %d, disabled %d/%x, desktopMode %d, ac %d\n",
2884 			    clamshellClosed, clamshellDisabled, clamshellSleepDisableMask,
2885 			    desktopMode, acAdaptorConnected);
2886 		}
2887 
2888 		clock_get_calendar_absolute_and_microtime(&secs, &microsecs, &now);
2889 		logtime(secs);
2890 		gIOLastSleepTime.tv_sec  = secs;
2891 		gIOLastSleepTime.tv_usec = microsecs;
2892 		if (!_aotLastWakeTime) {
2893 			gIOLastUserSleepTime = gIOLastSleepTime;
2894 		}
2895 
2896 		gIOLastWakeTime.tv_sec = 0;
2897 		gIOLastWakeTime.tv_usec = 0;
2898 		gIOLastSleepAbsTime = now;
2899 
2900 		if (wake2DarkwakeDelay && sleepDelaysReport) {
2901 			clock_sec_t     wake2DarkwakeSecs, darkwake2SleepSecs;
2902 			// Update 'wake2DarkwakeDelay' histogram if this is a fullwake->sleep transition
2903 
2904 			SUB_ABSOLUTETIME(&now, &ts_sleepStart);
2905 			absolutetime_to_microtime(now, &darkwake2SleepSecs, &microsecs);
2906 			absolutetime_to_microtime(wake2DarkwakeDelay, &wake2DarkwakeSecs, &microsecs);
2907 			HISTREPORT_TALLYVALUE(sleepDelaysReport,
2908 			    (int64_t)(wake2DarkwakeSecs + darkwake2SleepSecs));
2909 
2910 			DLOG("Updated sleepDelaysReport %lu %lu\n", (unsigned long)wake2DarkwakeSecs, (unsigned long)darkwake2SleepSecs);
2911 			wake2DarkwakeDelay = 0;
2912 		}
2913 #if HIBERNATION
2914 		LOG("System %sSleep\n", gIOHibernateState ? "Safe" : "");
2915 #if (DEVELOPMENT || DEBUG)
2916 		record_system_event(SYSTEM_EVENT_TYPE_INFO,
2917 		    SYSTEM_EVENT_SUBSYSTEM_PMRD,
2918 		    "System State",
2919 		    gIOHibernateState ? "Enter Hibernate" : "Enter Sleep"
2920 		    );
2921 #endif /* DEVELOPMENT || DEBUG */
2922 		IOHibernateSystemHasSlept();
2923 
2924 		evaluateSystemSleepPolicyFinal();
2925 #else
2926 		LOG("System Sleep\n");
2927 #if (DEVELOPMENT || DEBUG)
2928 		record_system_event(SYSTEM_EVENT_TYPE_INFO,
2929 		    SYSTEM_EVENT_SUBSYSTEM_PMRD,
2930 		    "System State", "Enter Sleep");
2931 #endif /* DEVELOPMENT || DEBUG */
2932 #endif
2933 		if (thermalWarningState) {
2934 			OSSharedPtr<const OSSymbol> event = OSSymbol::withCString(kIOPMThermalLevelWarningKey);
2935 			if (event) {
2936 				systemPowerEventOccurred(event.get(), kIOPMThermalLevelUnknown);
2937 			}
2938 		}
2939 		assertOnWakeSecs = 0;
2940 		lowBatteryCondition = false;
2941 		thermalEmergencyState = false;
2942 
2943 #if DEVELOPMENT || DEBUG
2944 		extern int g_should_log_clock_adjustments;
2945 		if (g_should_log_clock_adjustments) {
2946 			clock_sec_t  secs = 0;
2947 			clock_usec_t microsecs = 0;
2948 			uint64_t now_b = mach_absolute_time();
2949 
2950 			secs = 0;
2951 			microsecs = 0;
2952 			PEGetUTCTimeOfDay(&secs, &microsecs);
2953 
2954 			uint64_t now_a = mach_absolute_time();
2955 			os_log(OS_LOG_DEFAULT, "%s PMU before going to sleep %lu s %d u %llu abs_b_PEG %llu abs_a_PEG \n",
2956 			    __func__, (unsigned long)secs, microsecs, now_b, now_a);
2957 		}
2958 #endif
2959 
2960 		getPlatform()->sleepKernel();
2961 
2962 		// The CPU(s) are off at this point,
2963 		// Code will resume execution here upon wake.
2964 
2965 		clock_get_uptime(&gIOLastWakeAbsTime);
2966 		IOLog("gIOLastWakeAbsTime: %lld\n", gIOLastWakeAbsTime);
2967 #if DEVELOPMENT || DEBUG
2968 		record_system_event(SYSTEM_EVENT_TYPE_INFO,
2969 		    SYSTEM_EVENT_SUBSYSTEM_PMRD,
2970 		    "System State", "Waking Up"
2971 		    );
2972 #endif /* DEVELOPMENT || DEBUG */
2973 		_highestCapability = 0;
2974 
2975 #if HIBERNATION
2976 		IOHibernateSystemWake();
2977 #endif
2978 
2979 		// sleep transition complete
2980 		gSleepOrShutdownPending = 0;
2981 
2982 		// trip the reset of the calendar clock
2983 		clock_wakeup_calendar();
2984 		clock_get_calendar_microtime(&secs, &microsecs);
2985 		gIOLastWakeTime.tv_sec  = secs;
2986 		gIOLastWakeTime.tv_usec = microsecs;
2987 
2988 		// aot
2989 		if (_aotWakeTimeCalendar.selector != kPMCalendarTypeInvalid) {
2990 			_aotWakeTimeCalendar.selector = kPMCalendarTypeInvalid;
2991 			secs = 0;
2992 			microsecs = 0;
2993 			PEGetUTCTimeOfDay(&secs, &microsecs);
2994 			IOPMConvertSecondsToCalendar(secs, &nowCalendar);
2995 			IOLog("aotWake at " YMDTF " sched: " YMDTF "\n", YMDT(&nowCalendar), YMDT(&_aotWakeTimeCalendar));
2996 			_aotMetrics->sleepCount++;
2997 			_aotLastWakeTime = gIOLastWakeAbsTime;
2998 			if (_aotMetrics->sleepCount <= kIOPMAOTMetricsKernelWakeCountMax) {
2999 				_aotMetrics->kernelSleepTime[_aotMetrics->sleepCount - 1]
3000 				        = (((uint64_t) gIOLastSleepTime.tv_sec) << 10) + (gIOLastSleepTime.tv_usec / 1000);
3001 				_aotMetrics->kernelWakeTime[_aotMetrics->sleepCount - 1]
3002 				        = (((uint64_t) gIOLastWakeTime.tv_sec) << 10) + (gIOLastWakeTime.tv_usec / 1000);
3003 			}
3004 
3005 			if (_aotTestTime) {
3006 				if (_aotWakeTimeUTC <= secs) {
3007 					_aotTestTime = _aotTestTime + _aotTestInterval;
3008 				}
3009 				setWakeTime(_aotTestTime);
3010 			}
3011 		}
3012 
3013 #if HIBERNATION
3014 		LOG("System %sWake\n", gIOHibernateState ? "SafeSleep " : "");
3015 #endif
3016 
3017 		lastSleepReason = 0;
3018 
3019 		lastDebugWakeSeconds    = _debugWakeSeconds;
3020 		_debugWakeSeconds       = 0;
3021 		_scheduledAlarmMask     = 0;
3022 		_nextScheduledAlarmType = NULL;
3023 
3024 		darkWakeExit            = false;
3025 		darkWakePowerClamped    = false;
3026 		darkWakePostTickle      = false;
3027 		darkWakeHibernateError  = false;
3028 		darkWakeToSleepASAP     = true;
3029 		darkWakeLogClamp        = true;
3030 		sleepTimerMaintenance   = false;
3031 		sleepToStandby          = false;
3032 		wranglerTickled         = false;
3033 		userWasActive           = false;
3034 		isRTCAlarmWake          = false;
3035 		clamshellIgnoreClose    = false;
3036 		fullWakeReason = kFullWakeReasonNone;
3037 		idleSleepRevertible     = true;
3038 
3039 #if defined(__i386__) || defined(__x86_64__)
3040 		kdebugTrace(kPMLogSystemWake, 0, 0, 0);
3041 
3042 		OSSharedPtr<OSObject> wakeTypeProp   = copyProperty(kIOPMRootDomainWakeTypeKey);
3043 		OSSharedPtr<OSObject> wakeReasonProp = copyProperty(kIOPMRootDomainWakeReasonKey);
3044 		OSString * wakeType = OSDynamicCast(OSString, wakeTypeProp.get());
3045 		OSString * wakeReason = OSDynamicCast(OSString, wakeReasonProp.get());
3046 
3047 		if (wakeReason && (wakeReason->getLength() >= 2) &&
3048 		    gWakeReasonString[0] == '\0') {
3049 			WAKEEVENT_LOCK();
3050 			// Until the platform driver can claim its wake reasons
3051 			strlcat(gWakeReasonString, wakeReason->getCStringNoCopy(),
3052 			    sizeof(gWakeReasonString));
3053 			if (!gWakeReasonSysctlRegistered) {
3054 				gWakeReasonSysctlRegistered = true;
3055 			}
3056 			WAKEEVENT_UNLOCK();
3057 		}
3058 
3059 		if (wakeType && wakeType->isEqualTo(kIOPMrootDomainWakeTypeLowBattery)) {
3060 			lowBatteryCondition = true;
3061 			darkWakeMaintenance = true;
3062 		} else {
3063 #if HIBERNATION
3064 			OSSharedPtr<OSObject> hibOptionsProp = copyProperty(kIOHibernateOptionsKey);
3065 			OSNumber * hibOptions = OSDynamicCast(  OSNumber, hibOptionsProp.get());
3066 			if (hibernateAborted || ((hibOptions &&
3067 			    !(hibOptions->unsigned32BitValue() & kIOHibernateOptionDarkWake)))) {
3068 				// Hibernate aborted, or EFI brought up graphics
3069 				darkWakeExit = true;
3070 				if (hibernateAborted) {
3071 					DLOG("Hibernation aborted\n");
3072 				} else {
3073 					DLOG("EFI brought up graphics. Going to full wake. HibOptions: 0x%x\n", hibOptions->unsigned32BitValue());
3074 				}
3075 			} else
3076 #endif
3077 			if (wakeType && (
3078 				    wakeType->isEqualTo(kIOPMRootDomainWakeTypeUser) ||
3079 				    wakeType->isEqualTo(kIOPMRootDomainWakeTypeAlarm))) {
3080 				// User wake or RTC alarm
3081 				darkWakeExit = true;
3082 				if (wakeType->isEqualTo(kIOPMRootDomainWakeTypeAlarm)) {
3083 					isRTCAlarmWake = true;
3084 				}
3085 			} else if (wakeType &&
3086 			    wakeType->isEqualTo(kIOPMRootDomainWakeTypeSleepTimer)) {
3087 				// SMC standby timer trumps SleepX
3088 				darkWakeMaintenance = true;
3089 				sleepTimerMaintenance = true;
3090 			} else if ((lastDebugWakeSeconds != 0) &&
3091 			    ((gDarkWakeFlags & kDarkWakeFlagAlarmIsDark) == 0)) {
3092 				// SleepX before maintenance
3093 				darkWakeExit = true;
3094 			} else if (wakeType &&
3095 			    wakeType->isEqualTo(kIOPMRootDomainWakeTypeMaintenance)) {
3096 				darkWakeMaintenance = true;
3097 			} else if (wakeType &&
3098 			    wakeType->isEqualTo(kIOPMRootDomainWakeTypeSleepService)) {
3099 				darkWakeMaintenance = true;
3100 				darkWakeSleepService = true;
3101 #if HIBERNATION
3102 				if (kIOHibernateStateWakingFromHibernate == gIOHibernateState) {
3103 					sleepToStandby = true;
3104 				}
3105 #endif
3106 			} else if (wakeType &&
3107 			    wakeType->isEqualTo(kIOPMRootDomainWakeTypeHibernateError)) {
3108 				darkWakeMaintenance = true;
3109 				darkWakeHibernateError = true;
3110 			} else {
3111 				// Unidentified wake source, resume to full wake if debug
3112 				// alarm is pending.
3113 
3114 				if (lastDebugWakeSeconds &&
3115 				    (!wakeReason || wakeReason->isEqualTo(""))) {
3116 					darkWakeExit = true;
3117 				}
3118 			}
3119 		}
3120 
3121 		if (darkWakeExit) {
3122 			darkWakeToSleepASAP = false;
3123 			fullWakeReason = kFullWakeReasonLocalUser;
3124 			reportUserInput();
3125 		} else if (displayPowerOnRequested && checkSystemCanSustainFullWake()) {
3126 			handleSetDisplayPowerOn(true);
3127 		} else if (!darkWakeMaintenance) {
3128 			// Early/late tickle for non-maintenance wake.
3129 			if ((gDarkWakeFlags & kDarkWakeFlagPromotionMask) != kDarkWakeFlagPromotionNone) {
3130 				darkWakePostTickle = true;
3131 			}
3132 		}
3133 #else   /* !__i386__ && !__x86_64__ */
3134 		timeSinceReset = ml_get_time_since_reset();
3135 		kdebugTrace(kPMLogSystemWake, 0, (uintptr_t)(timeSinceReset >> 32), (uintptr_t) timeSinceReset);
3136 
3137 		if ((gDarkWakeFlags & kDarkWakeFlagPromotionMask) == kDarkWakeFlagPromotionEarly) {
3138 			wranglerTickled = true;
3139 			fullWakeReason = kFullWakeReasonLocalUser;
3140 			requestUserActive(this, "Full wake on dark wake promotion boot-arg");
3141 		} else if ((lastDebugWakeSeconds != 0) && !(gDarkWakeFlags & kDarkWakeFlagAlarmIsDark)) {
3142 			isRTCAlarmWake = true;
3143 			fullWakeReason = kFullWakeReasonLocalUser;
3144 			requestUserActive(this, "RTC debug alarm");
3145 		} else {
3146 #if HIBERNATION
3147 			OSSharedPtr<OSObject> hibOptionsProp = copyProperty(kIOHibernateOptionsKey);
3148 			OSNumber * hibOptions = OSDynamicCast(OSNumber, hibOptionsProp.get());
3149 			if (hibOptions && !(hibOptions->unsigned32BitValue() & kIOHibernateOptionDarkWake)) {
3150 				fullWakeReason = kFullWakeReasonLocalUser;
3151 				requestUserActive(this, "hibernate user wake");
3152 			}
3153 #endif
3154 		}
3155 
3156 		// stay awake for at least 30 seconds
3157 		startIdleSleepTimer(30 * 1000);
3158 #endif
3159 		sleepCnt++;
3160 
3161 		thread_call_enter(updateConsoleUsersEntry);
3162 
3163 		// Skip AOT_STATE if we are waking up from an RTC timer.
3164 		// This check needs to be done after the epoch change is processed
3165 		// and before the changePowerStateWithTagToPriv() call below.
3166 		WAKEEVENT_LOCK();
3167 		aotShouldExit(false);
3168 		WAKEEVENT_UNLOCK();
3169 
3170 		changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonWake);
3171 		break;
3172 	}
3173 #if !__i386__ && !__x86_64__
3174 	case ON_STATE:
3175 	case AOT_STATE:
3176 	{
3177 		DLOG("Force re-evaluating aggressiveness\n");
3178 		/* Force re-evaluate the aggressiveness values to set appropriate idle sleep timer */
3179 		pmPowerStateQueue->submitPowerEvent(
3180 			kPowerEventPolicyStimulus,
3181 			(void *) kStimulusNoIdleSleepPreventers );
3182 
3183 		// After changing to ON_STATE, invalidate any previously queued
3184 		// request to change to a state less than ON_STATE. This isn't
3185 		// necessary for AOT_STATE or if the device has only one running
3186 		// state since the changePowerStateToPriv() issued at the tail
3187 		// end of SLEEP_STATE case should take care of that.
3188 		if (getPowerState() == ON_STATE) {
3189 			changePowerStateWithTagToPriv(ON_STATE, kCPSReasonWake);
3190 		}
3191 		break;
3192 	}
3193 #endif /* !__i386__ && !__x86_64__ */
3194 	}
3195 	notifierThread = NULL;
3196 }
3197 
3198 //******************************************************************************
3199 // requestPowerDomainState
3200 //
3201 // Extend implementation in IOService. Running on PM work loop thread.
3202 //******************************************************************************
3203 
3204 IOReturn
requestPowerDomainState(IOPMPowerFlags childDesire,IOPowerConnection * childConnection,unsigned long specification)3205 IOPMrootDomain::requestPowerDomainState(
3206 	IOPMPowerFlags      childDesire,
3207 	IOPowerConnection * childConnection,
3208 	unsigned long       specification )
3209 {
3210 	// Idle and system sleep prevention flags affects driver desire.
3211 	// Children desire are irrelevant so they are cleared.
3212 
3213 	return super::requestPowerDomainState(0, childConnection, specification);
3214 }
3215 
3216 
3217 static void
makeSleepPreventersListLog(const OSSharedPtr<OSSet> & preventers,char * buf,size_t buf_size)3218 makeSleepPreventersListLog(const OSSharedPtr<OSSet> &preventers, char *buf, size_t buf_size)
3219 {
3220 	if (!preventers->getCount()) {
3221 		return;
3222 	}
3223 
3224 	char *buf_iter = buf + strlen(buf);
3225 	char *buf_end = buf + buf_size;
3226 
3227 	OSSharedPtr<OSCollectionIterator> iterator = OSCollectionIterator::withCollection(preventers.get());
3228 	OSObject *obj = NULL;
3229 
3230 	while ((obj = iterator->getNextObject())) {
3231 		IOService *srv = OSDynamicCast(IOService, obj);
3232 		if (buf_iter < buf_end) {
3233 			buf_iter += snprintf(buf_iter, buf_end - buf_iter, " %s", srv->getName());
3234 		} else {
3235 			DLOG("Print buffer exhausted for sleep preventers list\n");
3236 			break;
3237 		}
3238 	}
3239 }
3240 
3241 //******************************************************************************
3242 // updatePreventIdleSleepList
3243 //
3244 // Called by IOService on PM work loop.
3245 // Returns true if PM policy recognized the driver's desire to prevent idle
3246 // sleep and updated the list of idle sleep preventers. Returns false otherwise
3247 //******************************************************************************
3248 
3249 bool
updatePreventIdleSleepList(IOService * service,bool addNotRemove)3250 IOPMrootDomain::updatePreventIdleSleepList(
3251 	IOService * service, bool addNotRemove)
3252 {
3253 	unsigned int oldCount;
3254 
3255 	oldCount = idleSleepPreventersCount();
3256 	return updatePreventIdleSleepListInternal(service, addNotRemove, oldCount);
3257 }
3258 
3259 bool
updatePreventIdleSleepListInternal(IOService * service,bool addNotRemove,unsigned int oldCount)3260 IOPMrootDomain::updatePreventIdleSleepListInternal(
3261 	IOService * service, bool addNotRemove, unsigned int oldCount)
3262 {
3263 	unsigned int newCount;
3264 
3265 	ASSERT_GATED();
3266 
3267 #if defined(XNU_TARGET_OS_OSX)
3268 	// Only the display wrangler and no-idle-sleep kernel assertions
3269 	// can prevent idle sleep. The kIOPMPreventIdleSleep capability flag
3270 	// reported by drivers in their power state table is ignored.
3271 	if (service && (service != wrangler) && (service != this)) {
3272 		return false;
3273 	}
3274 #endif
3275 
3276 	if (service) {
3277 		if (addNotRemove) {
3278 			preventIdleSleepList->setObject(service);
3279 			DLOG("Added %s to idle sleep preventers list (Total %u)\n",
3280 			    service->getName(), preventIdleSleepList->getCount());
3281 		} else if (preventIdleSleepList->member(service)) {
3282 			preventIdleSleepList->removeObject(service);
3283 			DLOG("Removed %s from idle sleep preventers list (Total %u)\n",
3284 			    service->getName(), preventIdleSleepList->getCount());
3285 		}
3286 
3287 		if (preventIdleSleepList->getCount()) {
3288 			char buf[256] = "Idle Sleep Preventers:";
3289 			makeSleepPreventersListLog(preventIdleSleepList, buf, sizeof(buf));
3290 			DLOG("%s\n", buf);
3291 		}
3292 	}
3293 
3294 	newCount = idleSleepPreventersCount();
3295 
3296 	if ((oldCount == 0) && (newCount != 0)) {
3297 		// Driver added to empty prevent list.
3298 		// Update the driver desire to prevent idle sleep.
3299 		// Driver desire does not prevent demand sleep.
3300 
3301 		changePowerStateWithTagTo(getRUN_STATE(), kCPSReasonIdleSleepPrevent);
3302 	} else if ((oldCount != 0) && (newCount == 0)) {
3303 		// Last driver removed from prevent list.
3304 		// Drop the driver clamp to allow idle sleep.
3305 
3306 		changePowerStateWithTagTo(SLEEP_STATE, kCPSReasonIdleSleepAllow);
3307 		evaluatePolicy( kStimulusNoIdleSleepPreventers );
3308 	}
3309 	messageClient(kIOPMMessageIdleSleepPreventers, systemCapabilityNotifier.get(),
3310 	    &newCount, sizeof(newCount));
3311 
3312 #if defined(XNU_TARGET_OS_OSX)
3313 	if (addNotRemove && (service == wrangler) && !checkSystemCanSustainFullWake()) {
3314 		DLOG("Cannot cancel idle sleep\n");
3315 		return false; // do not idle-cancel
3316 	}
3317 #endif
3318 
3319 	return true;
3320 }
3321 
3322 //******************************************************************************
3323 // startSpinDump
3324 //******************************************************************************
3325 
3326 void
startSpinDump(uint32_t spindumpKind)3327 IOPMrootDomain::startSpinDump(uint32_t spindumpKind)
3328 {
3329 	messageClients(kIOPMMessageLaunchBootSpinDump, (void *)(uintptr_t)spindumpKind);
3330 }
3331 
3332 //******************************************************************************
3333 // preventSystemSleepListUpdate
3334 //
3335 // Called by IOService on PM work loop.
3336 //******************************************************************************
3337 
3338 void
updatePreventSystemSleepList(IOService * service,bool addNotRemove)3339 IOPMrootDomain::updatePreventSystemSleepList(
3340 	IOService * service, bool addNotRemove )
3341 {
3342 	unsigned int oldCount, newCount;
3343 
3344 	ASSERT_GATED();
3345 	if (this == service) {
3346 		return;
3347 	}
3348 
3349 	oldCount = preventSystemSleepList->getCount();
3350 	if (addNotRemove) {
3351 		preventSystemSleepList->setObject(service);
3352 		DLOG("Added %s to system sleep preventers list (Total %u)\n",
3353 		    service->getName(), preventSystemSleepList->getCount());
3354 		if (!assertOnWakeSecs && gIOLastWakeAbsTime) {
3355 			AbsoluteTime    now;
3356 			clock_usec_t    microsecs;
3357 			clock_get_uptime(&now);
3358 			SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
3359 			absolutetime_to_microtime(now, &assertOnWakeSecs, &microsecs);
3360 			if (assertOnWakeReport) {
3361 				HISTREPORT_TALLYVALUE(assertOnWakeReport, (int64_t)assertOnWakeSecs);
3362 				DLOG("Updated assertOnWake %lu\n", (unsigned long)assertOnWakeSecs);
3363 			}
3364 		}
3365 	} else if (preventSystemSleepList->member(service)) {
3366 		preventSystemSleepList->removeObject(service);
3367 		DLOG("Removed %s from system sleep preventers list (Total %u)\n",
3368 		    service->getName(), preventSystemSleepList->getCount());
3369 
3370 		if ((oldCount != 0) && (preventSystemSleepList->getCount() == 0)) {
3371 			// Lost all system sleep preventers.
3372 			// Send stimulus if system sleep was blocked, and is in dark wake.
3373 			evaluatePolicy( kStimulusDarkWakeEvaluate );
3374 		}
3375 	}
3376 
3377 	newCount = preventSystemSleepList->getCount();
3378 	if (newCount) {
3379 		char buf[256] = "System Sleep Preventers:";
3380 		makeSleepPreventersListLog(preventSystemSleepList, buf, sizeof(buf));
3381 		DLOG("%s\n", buf);
3382 	}
3383 
3384 	messageClient(kIOPMMessageSystemSleepPreventers, systemCapabilityNotifier.get(),
3385 	    &newCount, sizeof(newCount));
3386 }
3387 
3388 void
copySleepPreventersList(OSArray ** idleSleepList,OSArray ** systemSleepList)3389 IOPMrootDomain::copySleepPreventersList(OSArray **idleSleepList, OSArray **systemSleepList)
3390 {
3391 	OSSharedPtr<OSCollectionIterator> iterator;
3392 	OSObject    *object = NULL;
3393 	OSSharedPtr<OSArray>     array;
3394 
3395 	if (!gIOPMWorkLoop->inGate()) {
3396 		gIOPMWorkLoop->runAction(
3397 			OSMemberFunctionCast(IOWorkLoop::Action, this,
3398 			&IOPMrootDomain::IOPMrootDomain::copySleepPreventersList),
3399 			this, (void *)idleSleepList, (void *)systemSleepList);
3400 		return;
3401 	}
3402 
3403 	if (idleSleepList && preventIdleSleepList && (preventIdleSleepList->getCount() != 0)) {
3404 		iterator = OSCollectionIterator::withCollection(preventIdleSleepList.get());
3405 		array = OSArray::withCapacity(5);
3406 
3407 		if (iterator && array) {
3408 			while ((object = iterator->getNextObject())) {
3409 				IOService *service = OSDynamicCast(IOService, object);
3410 				if (service) {
3411 					OSSharedPtr<const OSSymbol> name = service->copyName();
3412 					if (name) {
3413 						array->setObject(name.get());
3414 					}
3415 				}
3416 			}
3417 		}
3418 		*idleSleepList = array.detach();
3419 	}
3420 
3421 	if (systemSleepList && preventSystemSleepList && (preventSystemSleepList->getCount() != 0)) {
3422 		iterator = OSCollectionIterator::withCollection(preventSystemSleepList.get());
3423 		array = OSArray::withCapacity(5);
3424 
3425 		if (iterator && array) {
3426 			while ((object = iterator->getNextObject())) {
3427 				IOService *service = OSDynamicCast(IOService, object);
3428 				if (service) {
3429 					OSSharedPtr<const OSSymbol> name = service->copyName();
3430 					if (name) {
3431 						array->setObject(name.get());
3432 					}
3433 				}
3434 			}
3435 		}
3436 		*systemSleepList = array.detach();
3437 	}
3438 }
3439 
3440 void
copySleepPreventersListWithID(OSArray ** idleSleepList,OSArray ** systemSleepList)3441 IOPMrootDomain::copySleepPreventersListWithID(OSArray **idleSleepList, OSArray **systemSleepList)
3442 {
3443 	OSSharedPtr<OSCollectionIterator> iterator;
3444 	OSObject    *object = NULL;
3445 	OSSharedPtr<OSArray>     array;
3446 
3447 	if (!gIOPMWorkLoop->inGate()) {
3448 		gIOPMWorkLoop->runAction(
3449 			OSMemberFunctionCast(IOWorkLoop::Action, this,
3450 			&IOPMrootDomain::IOPMrootDomain::copySleepPreventersListWithID),
3451 			this, (void *)idleSleepList, (void *)systemSleepList);
3452 		return;
3453 	}
3454 
3455 	if (idleSleepList && preventIdleSleepList && (preventIdleSleepList->getCount() != 0)) {
3456 		iterator = OSCollectionIterator::withCollection(preventIdleSleepList.get());
3457 		array = OSArray::withCapacity(5);
3458 
3459 		if (iterator && array) {
3460 			while ((object = iterator->getNextObject())) {
3461 				IOService *service = OSDynamicCast(IOService, object);
3462 				if (service) {
3463 					OSSharedPtr<OSDictionary> dict = OSDictionary::withCapacity(2);
3464 					OSSharedPtr<const OSSymbol> name = service->copyName();
3465 					OSSharedPtr<OSNumber> id = OSNumber::withNumber(service->getRegistryEntryID(), 64);
3466 					if (dict && name && id) {
3467 						dict->setObject(kIOPMDriverAssertionRegistryEntryIDKey, id.get());
3468 						dict->setObject(kIOPMDriverAssertionOwnerStringKey, name.get());
3469 						array->setObject(dict.get());
3470 					}
3471 				}
3472 			}
3473 		}
3474 		*idleSleepList = array.detach();
3475 	}
3476 
3477 	if (systemSleepList && preventSystemSleepList && (preventSystemSleepList->getCount() != 0)) {
3478 		iterator = OSCollectionIterator::withCollection(preventSystemSleepList.get());
3479 		array = OSArray::withCapacity(5);
3480 
3481 		if (iterator && array) {
3482 			while ((object = iterator->getNextObject())) {
3483 				IOService *service = OSDynamicCast(IOService, object);
3484 				if (service) {
3485 					OSSharedPtr<OSDictionary> dict = OSDictionary::withCapacity(2);
3486 					OSSharedPtr<const OSSymbol> name = service->copyName();
3487 					OSSharedPtr<OSNumber> id = OSNumber::withNumber(service->getRegistryEntryID(), 64);
3488 					if (dict && name && id) {
3489 						dict->setObject(kIOPMDriverAssertionRegistryEntryIDKey, id.get());
3490 						dict->setObject(kIOPMDriverAssertionOwnerStringKey, name.get());
3491 						array->setObject(dict.get());
3492 					}
3493 				}
3494 			}
3495 		}
3496 		*systemSleepList = array.detach();
3497 	}
3498 }
3499 
3500 //******************************************************************************
3501 // tellChangeDown
3502 //
3503 // Override the superclass implementation to send a different message type.
3504 //******************************************************************************
3505 
3506 bool
tellChangeDown(unsigned long stateNum)3507 IOPMrootDomain::tellChangeDown( unsigned long stateNum )
3508 {
3509 	DLOG("tellChangeDown %s->%s\n",
3510 	    getPowerStateString((uint32_t) getPowerState()), getPowerStateString((uint32_t) stateNum));
3511 
3512 	if (SLEEP_STATE == stateNum) {
3513 		// Legacy apps were already told in the full->dark transition
3514 		if (!ignoreTellChangeDown) {
3515 			tracePoint( kIOPMTracePointSleepApplications );
3516 		} else {
3517 			tracePoint( kIOPMTracePointSleepPriorityClients );
3518 		}
3519 	}
3520 
3521 	if (!ignoreTellChangeDown) {
3522 		userActivityAtSleep = userActivityCount;
3523 		DLOG("tellChangeDown::userActivityAtSleep %d\n", userActivityAtSleep);
3524 
3525 		if (SLEEP_STATE == stateNum) {
3526 			hibernateAborted = false;
3527 
3528 			// Direct callout into OSKext so it can disable kext unloads
3529 			// during sleep/wake to prevent deadlocks.
3530 			OSKextSystemSleepOrWake( kIOMessageSystemWillSleep );
3531 
3532 			IOService::updateConsoleUsers(NULL, kIOMessageSystemWillSleep);
3533 
3534 			// Two change downs are sent by IOServicePM. Ignore the 2nd.
3535 			// But tellClientsWithResponse() must be called for both.
3536 			ignoreTellChangeDown = true;
3537 		}
3538 	}
3539 
3540 	return super::tellClientsWithResponse( kIOMessageSystemWillSleep );
3541 }
3542 
3543 //******************************************************************************
3544 // askChangeDown
3545 //
3546 // Override the superclass implementation to send a different message type.
3547 // This must be idle sleep since we don't ask during any other power change.
3548 //******************************************************************************
3549 
3550 bool
askChangeDown(unsigned long stateNum)3551 IOPMrootDomain::askChangeDown( unsigned long stateNum )
3552 {
3553 	DLOG("askChangeDown %s->%s\n",
3554 	    getPowerStateString((uint32_t) getPowerState()), getPowerStateString((uint32_t) stateNum));
3555 
3556 	// Don't log for dark wake entry
3557 	if (kSystemTransitionSleep == _systemTransitionType) {
3558 		tracePoint( kIOPMTracePointSleepApplications );
3559 	}
3560 
3561 	return super::tellClientsWithResponse( kIOMessageCanSystemSleep );
3562 }
3563 
3564 //******************************************************************************
3565 // askChangeDownDone
3566 //
3567 // An opportunity for root domain to cancel the power transition,
3568 // possibily due to an assertion created by powerd in response to
3569 // kIOMessageCanSystemSleep.
3570 //
3571 // Idle sleep:
3572 //   full -> dark wake transition
3573 //     1. Notify apps and powerd with kIOMessageCanSystemSleep
3574 //     2. askChangeDownDone()
3575 //   dark -> sleep transition
3576 //     1. Notify powerd with kIOMessageCanSystemSleep
3577 //     2. askChangeDownDone()
3578 //
3579 // Demand sleep:
3580 //   full -> dark wake transition
3581 //     1. Notify powerd with kIOMessageCanSystemSleep
3582 //     2. askChangeDownDone()
3583 //   dark -> sleep transition
3584 //     1. Notify powerd with kIOMessageCanSystemSleep
3585 //     2. askChangeDownDone()
3586 //******************************************************************************
3587 
3588 void
askChangeDownDone(IOPMPowerChangeFlags * inOutChangeFlags,bool * cancel)3589 IOPMrootDomain::askChangeDownDone(
3590 	IOPMPowerChangeFlags * inOutChangeFlags, bool * cancel )
3591 {
3592 	DLOG("askChangeDownDone(0x%x, %u) type %x, cap %x->%x\n",
3593 	    *inOutChangeFlags, *cancel,
3594 	    _systemTransitionType,
3595 	    _currentCapability, _pendingCapability);
3596 
3597 	if ((false == *cancel) && (kSystemTransitionSleep == _systemTransitionType)) {
3598 		// Dark->Sleep transition.
3599 		// Check if there are any deny sleep assertions.
3600 		// lastSleepReason already set by handleOurPowerChangeStart()
3601 
3602 		if (!checkSystemCanSleep(lastSleepReason)) {
3603 			// Cancel dark wake to sleep transition.
3604 			// Must re-scan assertions upon entering dark wake.
3605 
3606 			*cancel = true;
3607 			DLOG("cancel dark->sleep\n");
3608 		}
3609 		if (_aotMode && (kPMCalendarTypeInvalid != _aotWakeTimeCalendar.selector)) {
3610 			uint64_t now = mach_continuous_time();
3611 			if (((now + _aotWakePreWindow) >= _aotWakeTimeContinuous)
3612 			    && (now < (_aotWakeTimeContinuous + _aotWakePostWindow))) {
3613 				*cancel = true;
3614 				IOLog("AOT wake window cancel: %qd, %qd\n", now, _aotWakeTimeContinuous);
3615 			}
3616 		}
3617 	}
3618 }
3619 
3620 //******************************************************************************
3621 // systemDidNotSleep
3622 //
3623 // Work common to both canceled or aborted sleep.
3624 //******************************************************************************
3625 
3626 void
systemDidNotSleep(void)3627 IOPMrootDomain::systemDidNotSleep( void )
3628 {
3629 	// reset console lock state
3630 	thread_call_enter(updateConsoleUsersEntry);
3631 
3632 	if (idleSleepEnabled) {
3633 		if (!wrangler) {
3634 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
3635 			startIdleSleepTimer(kIdleSleepRetryInterval);
3636 #else
3637 			startIdleSleepTimer(idleMilliSeconds);
3638 #endif
3639 		} else if (!userIsActive) {
3640 			// Manually start the idle sleep timer besides waiting for
3641 			// the user to become inactive.
3642 			startIdleSleepTimer(kIdleSleepRetryInterval);
3643 		}
3644 	}
3645 
3646 	preventTransitionToUserActive(false);
3647 	IOService::setAdvisoryTickleEnable( true );
3648 	idleSleepRevertible = true;
3649 
3650 	// After idle revert and cancel, send a did-change message to powerd
3651 	// to balance the previous will-change message. Kernel clients do not
3652 	// need this since sleep cannot be canceled once they are notified.
3653 
3654 	if (toldPowerdCapWillChange && systemCapabilityNotifier &&
3655 	    (_pendingCapability != _currentCapability) &&
3656 	    ((_systemMessageClientMask & kSystemMessageClientPowerd) != 0)) {
3657 		// Differs from a real capability gain change where notifyRef != 0,
3658 		// but it is zero here since no response is expected.
3659 
3660 		IOPMSystemCapabilityChangeParameters params;
3661 
3662 		bzero(&params, sizeof(params));
3663 		params.fromCapabilities = _pendingCapability;
3664 		params.toCapabilities = _currentCapability;
3665 		params.changeFlags = kIOPMSystemCapabilityDidChange;
3666 
3667 		DLOG("MESG cap %x->%x did change\n",
3668 		    params.fromCapabilities, params.toCapabilities);
3669 		messageClient(kIOMessageSystemCapabilityChange, systemCapabilityNotifier.get(),
3670 		    &params, sizeof(params));
3671 	}
3672 }
3673 
3674 //******************************************************************************
3675 // tellNoChangeDown
3676 //
3677 // Notify registered applications and kernel clients that we are not dropping
3678 // power.
3679 //
3680 // We override the superclass implementation so we can send a different message
3681 // type to the client or application being notified.
3682 //
3683 // This must be a vetoed idle sleep, since no other power change can be vetoed.
3684 //******************************************************************************
3685 
3686 void
tellNoChangeDown(unsigned long stateNum)3687 IOPMrootDomain::tellNoChangeDown( unsigned long stateNum )
3688 {
3689 	DLOG("tellNoChangeDown %s->%s\n",
3690 	    getPowerStateString((uint32_t) getPowerState()), getPowerStateString((uint32_t) stateNum));
3691 
3692 	// Sleep canceled, clear the sleep trace point.
3693 	tracePoint(kIOPMTracePointSystemUp);
3694 
3695 	systemDidNotSleep();
3696 	return tellClients( kIOMessageSystemWillNotSleep );
3697 }
3698 
3699 //******************************************************************************
3700 // tellChangeUp
3701 //
3702 // Notify registered applications and kernel clients that we are raising power.
3703 //
3704 // We override the superclass implementation so we can send a different message
3705 // type to the client or application being notified.
3706 //******************************************************************************
3707 
3708 void
tellChangeUp(unsigned long stateNum)3709 IOPMrootDomain::tellChangeUp( unsigned long stateNum )
3710 {
3711 	DLOG("tellChangeUp %s->%s\n",
3712 	    getPowerStateString((uint32_t) getPowerState()), getPowerStateString((uint32_t) stateNum));
3713 
3714 	ignoreTellChangeDown = false;
3715 
3716 	if (stateNum == ON_STATE) {
3717 		// Direct callout into OSKext so it can disable kext unloads
3718 		// during sleep/wake to prevent deadlocks.
3719 		OSKextSystemSleepOrWake( kIOMessageSystemHasPoweredOn );
3720 
3721 		// Notify platform that sleep was cancelled or resumed.
3722 		getPlatform()->callPlatformFunction(
3723 			sleepMessagePEFunction.get(), false,
3724 			(void *)(uintptr_t) kIOMessageSystemHasPoweredOn,
3725 			NULL, NULL, NULL);
3726 
3727 		if (getPowerState() == ON_STATE) {
3728 			// Sleep was cancelled by idle cancel or revert
3729 			if (!CAP_CURRENT(kIOPMSystemCapabilityGraphics)) {
3730 				// rdar://problem/50363791
3731 				// If system is in dark wake and sleep is cancelled, do not
3732 				// send SystemWillPowerOn/HasPoweredOn messages to kernel
3733 				// priority clients. They haven't yet seen a SystemWillSleep
3734 				// message before the cancellation. So make sure the kernel
3735 				// client bit is cleared in _systemMessageClientMask before
3736 				// invoking the tellClients() below. This bit may have been
3737 				// set by handleOurPowerChangeStart() anticipating a successful
3738 				// sleep and setting the filter mask ahead of time allows the
3739 				// SystemWillSleep message to go through.
3740 				_systemMessageClientMask &= ~kSystemMessageClientKernel;
3741 			}
3742 
3743 			systemDidNotSleep();
3744 			tellClients( kIOMessageSystemWillPowerOn );
3745 		}
3746 
3747 		tracePoint( kIOPMTracePointWakeApplications );
3748 		tellClients( kIOMessageSystemHasPoweredOn );
3749 	} else if (stateNum == AOT_STATE) {
3750 		if (getPowerState() == AOT_STATE) {
3751 			// Sleep was cancelled by idle cancel or revert
3752 			startIdleSleepTimer(idleMilliSeconds);
3753 		}
3754 	}
3755 }
3756 
3757 #define CAP_WILL_CHANGE_TO_OFF(params, flag) \
3758     (((params)->changeFlags & kIOPMSystemCapabilityWillChange) && \
3759      ((params)->fromCapabilities & (flag)) && \
3760      (((params)->toCapabilities & (flag)) == 0))
3761 
3762 #define CAP_DID_CHANGE_TO_ON(params, flag) \
3763     (((params)->changeFlags & kIOPMSystemCapabilityDidChange) && \
3764      ((params)->toCapabilities & (flag)) && \
3765      (((params)->fromCapabilities & (flag)) == 0))
3766 
3767 #define CAP_DID_CHANGE_TO_OFF(params, flag) \
3768     (((params)->changeFlags & kIOPMSystemCapabilityDidChange) && \
3769      ((params)->fromCapabilities & (flag)) && \
3770      (((params)->toCapabilities & (flag)) == 0))
3771 
3772 #define CAP_WILL_CHANGE_TO_ON(params, flag) \
3773     (((params)->changeFlags & kIOPMSystemCapabilityWillChange) && \
3774      ((params)->toCapabilities & (flag)) && \
3775      (((params)->fromCapabilities & (flag)) == 0))
3776 
3777 //******************************************************************************
3778 // sysPowerDownHandler
3779 //
3780 // Perform a vfs sync before system sleep.
3781 //******************************************************************************
3782 
3783 IOReturn
sysPowerDownHandler(void * target,void * refCon,UInt32 messageType,IOService * service,void * messageArgs,vm_size_t argSize)3784 IOPMrootDomain::sysPowerDownHandler(
3785 	void * target, void * refCon,
3786 	UInt32 messageType, IOService * service,
3787 	void * messageArgs, vm_size_t argSize )
3788 {
3789 	static UInt32 lastSystemMessageType = 0;
3790 	IOReturn    ret = 0;
3791 
3792 	DLOG("sysPowerDownHandler message %s\n", getIOMessageString(messageType));
3793 
3794 	// rdar://problem/50363791
3795 	// Sanity check to make sure the SystemWill/Has message types are
3796 	// received in the expected order for all kernel priority clients.
3797 	if (messageType == kIOMessageSystemWillSleep ||
3798 	    messageType == kIOMessageSystemWillPowerOn ||
3799 	    messageType == kIOMessageSystemHasPoweredOn) {
3800 		switch (messageType) {
3801 		case kIOMessageSystemWillPowerOn:
3802 			assert(lastSystemMessageType == kIOMessageSystemWillSleep);
3803 			break;
3804 		case kIOMessageSystemHasPoweredOn:
3805 			assert(lastSystemMessageType == kIOMessageSystemWillPowerOn);
3806 			break;
3807 		}
3808 
3809 		lastSystemMessageType = messageType;
3810 	}
3811 
3812 	if (!gRootDomain) {
3813 		return kIOReturnUnsupported;
3814 	}
3815 
3816 	if (messageType == kIOMessageSystemCapabilityChange) {
3817 		IOPMSystemCapabilityChangeParameters * params =
3818 		    (IOPMSystemCapabilityChangeParameters *) messageArgs;
3819 
3820 		// Interested applications have been notified of an impending power
3821 		// change and have acked (when applicable).
3822 		// This is our chance to save whatever state we can before powering
3823 		// down.
3824 		// We call sync_internal defined in xnu/bsd/vfs/vfs_syscalls.c,
3825 		// via callout
3826 
3827 		DLOG("sysPowerDownHandler cap %x -> %x (flags %x)\n",
3828 		    params->fromCapabilities, params->toCapabilities,
3829 		    params->changeFlags);
3830 
3831 		if (CAP_WILL_CHANGE_TO_OFF(params, kIOPMSystemCapabilityCPU)) {
3832 			// We will ack within 20 seconds
3833 			params->maxWaitForReply = 20 * 1000 * 1000;
3834 
3835 #if HIBERNATION
3836 			gRootDomain->evaluateSystemSleepPolicyEarly();
3837 
3838 			// add in time we could spend freeing pages
3839 			if (gRootDomain->hibernateMode && !gRootDomain->hibernateDisabled) {
3840 				params->maxWaitForReply = kCapabilityClientMaxWait;
3841 			}
3842 			DLOG("sysPowerDownHandler max wait %d s\n",
3843 			    (int) (params->maxWaitForReply / 1000 / 1000));
3844 #endif
3845 
3846 			// Notify platform that sleep has begun, after the early
3847 			// sleep policy evaluation.
3848 			getPlatform()->callPlatformFunction(
3849 				sleepMessagePEFunction.get(), false,
3850 				(void *)(uintptr_t) kIOMessageSystemWillSleep,
3851 				NULL, NULL, NULL);
3852 
3853 			if (!OSCompareAndSwap( 0, 1, &gSleepOrShutdownPending )) {
3854 				// Purposely delay the ack and hope that shutdown occurs quickly.
3855 				// Another option is not to schedule the thread and wait for
3856 				// ack timeout...
3857 				AbsoluteTime deadline;
3858 				clock_interval_to_deadline( 30, kSecondScale, &deadline );
3859 				thread_call_enter1_delayed(
3860 					gRootDomain->diskSyncCalloutEntry,
3861 					(thread_call_param_t)(uintptr_t) params->notifyRef,
3862 					deadline );
3863 			} else {
3864 				thread_call_enter1(
3865 					gRootDomain->diskSyncCalloutEntry,
3866 					(thread_call_param_t)(uintptr_t) params->notifyRef);
3867 			}
3868 		}
3869 #if HIBERNATION
3870 		else if (CAP_DID_CHANGE_TO_ON(params, kIOPMSystemCapabilityCPU)) {
3871 			// We will ack within 110 seconds
3872 			params->maxWaitForReply = 110 * 1000 * 1000;
3873 
3874 			thread_call_enter1(
3875 				gRootDomain->diskSyncCalloutEntry,
3876 				(thread_call_param_t)(uintptr_t) params->notifyRef);
3877 		}
3878 #endif
3879 		ret = kIOReturnSuccess;
3880 	}
3881 
3882 	return ret;
3883 }
3884 
3885 //******************************************************************************
3886 // handleQueueSleepWakeUUID
3887 //
3888 // Called from IOPMrootDomain when we're initiating a sleep,
3889 // or indirectly from PM configd when PM decides to clear the UUID.
3890 // PM clears the UUID several minutes after successful wake from sleep,
3891 // so that we might associate App spindumps with the immediately previous
3892 // sleep/wake.
3893 //
3894 // @param   obj has a retain on it. We're responsible for releasing that retain.
3895 //******************************************************************************
3896 
3897 void
handleQueueSleepWakeUUID(OSObject * obj)3898 IOPMrootDomain::handleQueueSleepWakeUUID(OSObject *obj)
3899 {
3900 	OSSharedPtr<OSString>    str;
3901 
3902 	if (kOSBooleanFalse == obj) {
3903 		handlePublishSleepWakeUUID(false);
3904 	} else {
3905 		str.reset(OSDynamicCast(OSString, obj), OSNoRetain);
3906 		if (str) {
3907 			// This branch caches the UUID for an upcoming sleep/wake
3908 			queuedSleepWakeUUIDString = str;
3909 			DLOG("SleepWake UUID queued: %s\n", queuedSleepWakeUUIDString->getCStringNoCopy());
3910 		}
3911 	}
3912 }
3913 //******************************************************************************
3914 // handlePublishSleepWakeUUID
3915 //
3916 // Called from IOPMrootDomain when we're initiating a sleep,
3917 // or indirectly from PM configd when PM decides to clear the UUID.
3918 // PM clears the UUID several minutes after successful wake from sleep,
3919 // so that we might associate App spindumps with the immediately previous
3920 // sleep/wake.
3921 //******************************************************************************
3922 
3923 void
handlePublishSleepWakeUUID(bool shouldPublish)3924 IOPMrootDomain::handlePublishSleepWakeUUID( bool shouldPublish )
3925 {
3926 	ASSERT_GATED();
3927 
3928 	/*
3929 	 * Clear the current UUID
3930 	 */
3931 	if (gSleepWakeUUIDIsSet) {
3932 		DLOG("SleepWake UUID cleared\n");
3933 
3934 		gSleepWakeUUIDIsSet = false;
3935 
3936 		removeProperty(kIOPMSleepWakeUUIDKey);
3937 		messageClients(kIOPMMessageSleepWakeUUIDChange, kIOPMMessageSleepWakeUUIDCleared);
3938 	}
3939 
3940 	/*
3941 	 * Optionally, publish a new UUID
3942 	 */
3943 	if (queuedSleepWakeUUIDString && shouldPublish) {
3944 		OSSharedPtr<OSString> publishThisUUID;
3945 
3946 		publishThisUUID = queuedSleepWakeUUIDString;
3947 
3948 		if (publishThisUUID) {
3949 			setProperty(kIOPMSleepWakeUUIDKey, publishThisUUID.get());
3950 		}
3951 
3952 		gSleepWakeUUIDIsSet = true;
3953 		messageClients(kIOPMMessageSleepWakeUUIDChange, kIOPMMessageSleepWakeUUIDSet);
3954 
3955 		queuedSleepWakeUUIDString.reset();
3956 	}
3957 }
3958 
3959 //******************************************************************************
3960 // IOPMGetSleepWakeUUIDKey
3961 //
3962 // Return the truth value of gSleepWakeUUIDIsSet and optionally copy the key.
3963 // To get the full key -- a C string -- the buffer must large enough for
3964 // the end-of-string character.
3965 // The key is expected to be an UUID string
3966 //******************************************************************************
3967 
3968 extern "C" bool
IOPMCopySleepWakeUUIDKey(char * buffer,size_t buf_len)3969 IOPMCopySleepWakeUUIDKey(char *buffer, size_t buf_len)
3970 {
3971 	if (!gSleepWakeUUIDIsSet) {
3972 		return false;
3973 	}
3974 
3975 	if (buffer != NULL) {
3976 		OSSharedPtr<OSString> string =
3977 		    OSDynamicPtrCast<OSString>(gRootDomain->copyProperty(kIOPMSleepWakeUUIDKey));
3978 
3979 		if (!string) {
3980 			*buffer = '\0';
3981 		} else {
3982 			strlcpy(buffer, string->getCStringNoCopy(), buf_len);
3983 		}
3984 	}
3985 
3986 	return true;
3987 }
3988 
3989 //******************************************************************************
3990 // lowLatencyAudioNotify
3991 //
3992 // Used to send an update about low latency audio activity to interested
3993 // clients. To keep the overhead minimal the OSDictionary used here
3994 // is initialized at boot.
3995 //******************************************************************************
3996 
3997 void
lowLatencyAudioNotify(uint64_t time,boolean_t state)3998 IOPMrootDomain::lowLatencyAudioNotify(uint64_t time, boolean_t state)
3999 {
4000 	if (lowLatencyAudioNotifierDict && lowLatencyAudioNotifyStateSym && lowLatencyAudioNotifyTimestampSym &&
4001 	    lowLatencyAudioNotifyStateVal && lowLatencyAudioNotifyTimestampVal) {
4002 		lowLatencyAudioNotifyTimestampVal->setValue(time);
4003 		lowLatencyAudioNotifyStateVal->setValue(state);
4004 		setPMSetting(gIOPMSettingLowLatencyAudioModeKey.get(), lowLatencyAudioNotifierDict.get());
4005 	} else {
4006 		DLOG("LowLatencyAudioNotify error\n");
4007 	}
4008 	return;
4009 }
4010 
4011 //******************************************************************************
4012 // IOPMrootDomainRTNotifier
4013 //
4014 // Used by performance controller to update the timestamp and state associated
4015 // with low latency audio activity in the system.
4016 //******************************************************************************
4017 
4018 extern "C" void
IOPMrootDomainRTNotifier(uint64_t time,boolean_t state)4019 IOPMrootDomainRTNotifier(uint64_t time, boolean_t state)
4020 {
4021 	gRootDomain->lowLatencyAudioNotify(time, state);
4022 	return;
4023 }
4024 
4025 //******************************************************************************
4026 // initializeBootSessionUUID
4027 //
4028 // Initialize the boot session uuid at boot up and sets it into registry.
4029 //******************************************************************************
4030 
4031 void
initializeBootSessionUUID(void)4032 IOPMrootDomain::initializeBootSessionUUID(void)
4033 {
4034 	uuid_t          new_uuid;
4035 	uuid_string_t   new_uuid_string;
4036 
4037 	uuid_generate(new_uuid);
4038 	uuid_unparse_upper(new_uuid, new_uuid_string);
4039 	memcpy(bootsessionuuid_string, new_uuid_string, sizeof(uuid_string_t));
4040 
4041 	setProperty(kIOPMBootSessionUUIDKey, new_uuid_string);
4042 }
4043 
4044 //******************************************************************************
4045 // Root domain uses the private and tagged changePowerState methods for
4046 // tracking and logging purposes.
4047 //******************************************************************************
4048 
4049 #define REQUEST_TAG_TO_REASON(x)        ((uint16_t)x)
4050 
4051 static uint32_t
nextRequestTag(IOPMRequestTag tag)4052 nextRequestTag( IOPMRequestTag tag )
4053 {
4054 	static SInt16 msb16 = 1;
4055 	uint16_t id = OSAddAtomic16(1, &msb16);
4056 	return ((uint32_t)id << 16) | REQUEST_TAG_TO_REASON(tag);
4057 }
4058 
4059 // TODO: remove this shim function and exported symbol
4060 IOReturn
changePowerStateTo(unsigned long ordinal)4061 IOPMrootDomain::changePowerStateTo( unsigned long ordinal )
4062 {
4063 	return changePowerStateWithTagTo(ordinal, kCPSReasonNone);
4064 }
4065 
4066 // TODO: remove this shim function and exported symbol
4067 IOReturn
changePowerStateToPriv(unsigned long ordinal)4068 IOPMrootDomain::changePowerStateToPriv( unsigned long ordinal )
4069 {
4070 	return changePowerStateWithTagToPriv(ordinal, kCPSReasonNone);
4071 }
4072 
4073 IOReturn
changePowerStateWithOverrideTo(IOPMPowerStateIndex ordinal,IOPMRequestTag reason)4074 IOPMrootDomain::changePowerStateWithOverrideTo(
4075 	IOPMPowerStateIndex ordinal, IOPMRequestTag reason )
4076 {
4077 	uint32_t tag = nextRequestTag(reason);
4078 	DLOG("%s(%s, %x)\n", __FUNCTION__, getPowerStateString((uint32_t) ordinal), tag);
4079 
4080 	if ((ordinal != ON_STATE) && (ordinal != AOT_STATE) && (ordinal != SLEEP_STATE)) {
4081 		return kIOReturnUnsupported;
4082 	}
4083 
4084 	return super::changePowerStateWithOverrideTo(ordinal, tag);
4085 }
4086 
4087 IOReturn
changePowerStateWithTagTo(IOPMPowerStateIndex ordinal,IOPMRequestTag reason)4088 IOPMrootDomain::changePowerStateWithTagTo(
4089 	IOPMPowerStateIndex ordinal, IOPMRequestTag reason )
4090 {
4091 	uint32_t tag = nextRequestTag(reason);
4092 	DLOG("%s(%s, %x)\n", __FUNCTION__, getPowerStateString((uint32_t) ordinal), tag);
4093 
4094 	if ((ordinal != ON_STATE) && (ordinal != AOT_STATE) && (ordinal != SLEEP_STATE)) {
4095 		return kIOReturnUnsupported;
4096 	}
4097 
4098 	return super::changePowerStateWithTagTo(ordinal, tag);
4099 }
4100 
4101 IOReturn
changePowerStateWithTagToPriv(IOPMPowerStateIndex ordinal,IOPMRequestTag reason)4102 IOPMrootDomain::changePowerStateWithTagToPriv(
4103 	IOPMPowerStateIndex ordinal, IOPMRequestTag reason )
4104 {
4105 	uint32_t tag = nextRequestTag(reason);
4106 	DLOG("%s(%s, %x)\n", __FUNCTION__, getPowerStateString((uint32_t) ordinal), tag);
4107 
4108 	if ((ordinal != ON_STATE) && (ordinal != AOT_STATE) && (ordinal != SLEEP_STATE)) {
4109 		return kIOReturnUnsupported;
4110 	}
4111 
4112 	return super::changePowerStateWithTagToPriv(ordinal, tag);
4113 }
4114 
4115 //******************************************************************************
4116 // activity detect
4117 //
4118 //******************************************************************************
4119 
4120 bool
activitySinceSleep(void)4121 IOPMrootDomain::activitySinceSleep(void)
4122 {
4123 	return userActivityCount != userActivityAtSleep;
4124 }
4125 
4126 bool
abortHibernation(void)4127 IOPMrootDomain::abortHibernation(void)
4128 {
4129 #if __arm64__
4130 	// don't allow hibernation to be aborted on ARM due to user activity
4131 	// since once ApplePMGR decides we're hibernating, we can't turn back
4132 	// see: <rdar://problem/63848862> Tonga ApplePMGR diff quiesce path support
4133 	return false;
4134 #else
4135 	bool ret = activitySinceSleep();
4136 
4137 	if (ret && !hibernateAborted && checkSystemCanSustainFullWake()) {
4138 		DLOG("activitySinceSleep ABORT [%d, %d]\n", userActivityCount, userActivityAtSleep);
4139 		hibernateAborted = true;
4140 	}
4141 	return ret;
4142 #endif
4143 }
4144 
4145 extern "C" int
hibernate_should_abort(void)4146 hibernate_should_abort(void)
4147 {
4148 	if (gRootDomain) {
4149 		return gRootDomain->abortHibernation();
4150 	} else {
4151 		return 0;
4152 	}
4153 }
4154 
4155 //******************************************************************************
4156 // scheduleImmediateDebugWake
4157 //
4158 // Schedule a wake with RTC to wake us back up immediately after we sleep.
4159 // Useful when a cancel request comes in past the revert point on the sleep path
4160 //******************************************************************************
4161 
4162 void
scheduleImmediateDebugWake(void)4163 IOPMrootDomain::scheduleImmediateDebugWake( void )
4164 {
4165 	OSSharedPtr<OSDictionary> dict = OSDictionary::withCapacity(1);
4166 	OSSharedPtr<OSNumber> secs = OSNumber::withNumber(1, 32);
4167 
4168 	if (dict && secs) {
4169 		dict->setObject(gIOPMSettingDebugWakeRelativeKey.get(), secs.get());
4170 		gRootDomain->setProperties(dict.get());
4171 		MSG("Reverting sleep with relative wake\n");
4172 	}
4173 }
4174 
4175 //******************************************************************************
4176 // willNotifyPowerChildren
4177 //
4178 // Called after all interested drivers have all acknowledged the power change,
4179 // but before any power children is informed. Dispatched though a thread call,
4180 // so it is safe to perform work that might block on a sleeping disk. PM state
4181 // machine (not thread) will block w/o timeout until this function returns.
4182 //******************************************************************************
4183 
4184 void
willNotifyPowerChildren(IOPMPowerStateIndex newPowerState)4185 IOPMrootDomain::willNotifyPowerChildren( IOPMPowerStateIndex newPowerState )
4186 {
4187 	if (SLEEP_STATE == newPowerState) {
4188 		notifierThread = current_thread();
4189 		if (updateTasksSuspend(kTasksSuspendSuspended, kTasksSuspendNoChange)) {
4190 			AbsoluteTime deadline;
4191 
4192 			clock_interval_to_deadline(10, kSecondScale, &deadline);
4193 #if defined(XNU_TARGET_OS_OSX)
4194 			vm_pageout_wait(AbsoluteTime_to_scalar(&deadline));
4195 #endif /* defined(XNU_TARGET_OS_OSX) */
4196 		}
4197 
4198 		_aotReadyToFullWake = false;
4199 #if 0
4200 		if (_aotLingerTime) {
4201 			uint64_t deadline;
4202 			IOLog("aot linger no return\n");
4203 			clock_absolutetime_interval_to_deadline(_aotLingerTime, &deadline);
4204 			clock_delay_until(deadline);
4205 		}
4206 #endif
4207 		if (!_aotMode) {
4208 			_aotTestTime = 0;
4209 			_aotWakeTimeCalendar.selector = kPMCalendarTypeInvalid;
4210 			_aotLastWakeTime = 0;
4211 			if (_aotMetrics) {
4212 				bzero(_aotMetrics, sizeof(IOPMAOTMetrics));
4213 			}
4214 		} else if (!_aotNow && !_debugWakeSeconds) {
4215 			_aotNow            = true;
4216 			_aotPendingFlags   = 0;
4217 			_aotTasksSuspended = true;
4218 			_aotLastWakeTime   = 0;
4219 			bzero(_aotMetrics, sizeof(IOPMAOTMetrics));
4220 			if (kIOPMAOTModeCycle & _aotMode) {
4221 				clock_interval_to_absolutetime_interval(60, kSecondScale, &_aotTestInterval);
4222 				_aotTestTime = mach_continuous_time() + _aotTestInterval;
4223 				setWakeTime(_aotTestTime);
4224 			}
4225 			uint32_t lingerSecs;
4226 			if (!PE_parse_boot_argn("aotlinger", &lingerSecs, sizeof(lingerSecs))) {
4227 				lingerSecs = 0;
4228 			}
4229 			clock_interval_to_absolutetime_interval(lingerSecs, kSecondScale, &_aotLingerTime);
4230 			clock_interval_to_absolutetime_interval(2000, kMillisecondScale, &_aotWakePreWindow);
4231 			clock_interval_to_absolutetime_interval(1100, kMillisecondScale, &_aotWakePostWindow);
4232 		}
4233 
4234 #if HIBERNATION
4235 		// Adjust watchdog for IOHibernateSystemSleep
4236 		int defaultTimeout = getWatchdogTimeout();
4237 		int timeout = defaultTimeout > WATCHDOG_HIBERNATION_TIMEOUT ?
4238 		    defaultTimeout : WATCHDOG_HIBERNATION_TIMEOUT;
4239 		reset_watchdog_timer(timeout);
4240 
4241 		IOHibernateSystemSleep();
4242 		IOHibernateIOKitSleep();
4243 #endif
4244 #if defined(__arm64__) && HIBERNATION
4245 		// On AS, hibernation cannot be aborted. Resetting RTC to 1s during hibernation upon detecting
4246 		// user activity is pointless (we are likely to spend >1s hibernating). It also clears existing
4247 		// alarms, which can mess with cycler tools.
4248 		if (gRootDomain->activitySinceSleep() && gIOHibernateState == kIOHibernateStateInactive) {
4249 #else /* defined(__arm64__) && HIBERNATION */
4250 		// On non-AS, hibernation can be aborted if user activity is detected. So continue to reset the
4251 		// RTC alarm (even during hibernation) so we can immediately wake from regular S2R if needed.
4252 		if (gRootDomain->activitySinceSleep()) {
4253 #endif /* defined(__arm64__) && HIBERNATION */
4254 			scheduleImmediateDebugWake();
4255 		}
4256 
4257 		notifierThread = NULL;
4258 	}
4259 }
4260 
4261 //******************************************************************************
4262 // willTellSystemCapabilityDidChange
4263 //
4264 // IOServicePM calls this from OurChangeTellCapabilityDidChange() when root
4265 // domain is raising its power state, immediately after notifying interested
4266 // drivers and power children.
4267 //******************************************************************************
4268 
4269 void
4270 IOPMrootDomain::willTellSystemCapabilityDidChange( void )
4271 {
4272 	if ((_systemTransitionType == kSystemTransitionWake) &&
4273 	    !CAP_GAIN(kIOPMSystemCapabilityGraphics)) {
4274 		// After powering up drivers, dark->full promotion on the current wake
4275 		// transition is no longer possible. That is because the next machine
4276 		// state will issue the system capability change messages.
4277 		// The darkWakePowerClamped flag may already be set if the system has
4278 		// at least one driver that was power clamped due to dark wake.
4279 		// This function sets the darkWakePowerClamped flag in case there
4280 		// is no power-clamped driver in the system.
4281 		//
4282 		// Last opportunity to exit dark wake using:
4283 		// requestFullWake( kFullWakeReasonLocalUser );
4284 
4285 		if (!darkWakePowerClamped) {
4286 			if (darkWakeLogClamp) {
4287 				AbsoluteTime    now;
4288 				uint64_t        nsec;
4289 
4290 				clock_get_uptime(&now);
4291 				SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
4292 				absolutetime_to_nanoseconds(now, &nsec);
4293 				DLOG("dark wake promotion disabled at %u ms\n",
4294 				    ((int)((nsec) / NSEC_PER_MSEC)));
4295 			}
4296 			darkWakePowerClamped = true;
4297 		}
4298 	}
4299 }
4300 
4301 //******************************************************************************
4302 // sleepOnClamshellClosed
4303 //
4304 // contains the logic to determine if the system should sleep when the clamshell
4305 // is closed.
4306 //******************************************************************************
4307 
4308 bool
4309 IOPMrootDomain::shouldSleepOnClamshellClosed( void )
4310 {
4311 	if (!clamshellExists) {
4312 		return false;
4313 	}
4314 
4315 	DLOG("clamshell closed %d, disabled %d/%x, desktopMode %d, ac %d\n",
4316 	    clamshellClosed, clamshellDisabled, clamshellSleepDisableMask, desktopMode, acAdaptorConnected);
4317 
4318 	return !clamshellDisabled && !(desktopMode && acAdaptorConnected) && !clamshellSleepDisableMask;
4319 }
4320 
4321 bool
4322 IOPMrootDomain::shouldSleepOnRTCAlarmWake( void )
4323 {
4324 	// Called once every RTC/Alarm wake. Device should go to sleep if on clamshell
4325 	// closed && battery
4326 	if (!clamshellExists) {
4327 		return false;
4328 	}
4329 
4330 	DLOG("shouldSleepOnRTCAlarmWake: clamshell closed %d, disabled %d/%x, desktopMode %d, ac %d\n",
4331 	    clamshellClosed, clamshellDisabled, clamshellSleepDisableMask, desktopMode, acAdaptorConnected);
4332 
4333 	return !acAdaptorConnected && !clamshellSleepDisableMask;
4334 }
4335 
4336 void
4337 IOPMrootDomain::sendClientClamshellNotification( void )
4338 {
4339 	/* Only broadcast clamshell alert if clamshell exists. */
4340 	if (!clamshellExists) {
4341 		return;
4342 	}
4343 
4344 	setProperty(kAppleClamshellStateKey,
4345 	    clamshellClosed ? kOSBooleanTrue : kOSBooleanFalse);
4346 
4347 	setProperty(kAppleClamshellCausesSleepKey,
4348 	    shouldSleepOnClamshellClosed() ? kOSBooleanTrue : kOSBooleanFalse);
4349 
4350 	/* Argument to message is a bitfiel of
4351 	 *      ( kClamshellStateBit | kClamshellSleepBit )
4352 	 */
4353 	messageClients(kIOPMMessageClamshellStateChange,
4354 	    (void *)(uintptr_t) ((clamshellClosed ? kClamshellStateBit : 0)
4355 	    | (shouldSleepOnClamshellClosed() ? kClamshellSleepBit : 0)));
4356 }
4357 
4358 //******************************************************************************
4359 // getSleepSupported
4360 //
4361 // Deprecated
4362 //******************************************************************************
4363 
4364 IOOptionBits
4365 IOPMrootDomain::getSleepSupported( void )
4366 {
4367 	return platformSleepSupport;
4368 }
4369 
4370 //******************************************************************************
4371 // setSleepSupported
4372 //
4373 // Deprecated
4374 //******************************************************************************
4375 
4376 void
4377 IOPMrootDomain::setSleepSupported( IOOptionBits flags )
4378 {
4379 	DLOG("setSleepSupported(%x)\n", (uint32_t) flags);
4380 	OSBitOrAtomic(flags, &platformSleepSupport);
4381 }
4382 
4383 //******************************************************************************
4384 // setClamShellSleepDisable
4385 //
4386 //******************************************************************************
4387 
4388 void
4389 IOPMrootDomain::setClamShellSleepDisable( bool disable, uint32_t bitmask )
4390 {
4391 	uint32_t oldMask;
4392 
4393 	// User client calls this in non-gated context
4394 	if (gIOPMWorkLoop->inGate() == false) {
4395 		gIOPMWorkLoop->runAction(
4396 			OSMemberFunctionCast(IOWorkLoop::Action, this,
4397 			&IOPMrootDomain::setClamShellSleepDisable),
4398 			(OSObject *) this,
4399 			(void *) disable, (void *)(uintptr_t) bitmask);
4400 		return;
4401 	}
4402 
4403 	oldMask = clamshellSleepDisableMask;
4404 	if (disable) {
4405 		clamshellSleepDisableMask |= bitmask;
4406 	} else {
4407 		clamshellSleepDisableMask &= ~bitmask;
4408 	}
4409 	DLOG("setClamShellSleepDisable(%x->%x)\n", oldMask, clamshellSleepDisableMask);
4410 
4411 	if (clamshellExists && clamshellClosed &&
4412 	    (clamshellSleepDisableMask != oldMask) &&
4413 	    (clamshellSleepDisableMask == 0)) {
4414 		handlePowerNotification(kLocalEvalClamshellCommand);
4415 	}
4416 }
4417 
4418 //******************************************************************************
4419 // wakeFromDoze
4420 //
4421 // Deprecated.
4422 //******************************************************************************
4423 
4424 void
4425 IOPMrootDomain::wakeFromDoze( void )
4426 {
4427 	// Preserve symbol for familes (IOUSBFamily and IOGraphics)
4428 }
4429 
4430 //******************************************************************************
4431 // recordRTCAlarm
4432 //
4433 // Record the earliest scheduled RTC alarm to determine whether a RTC wake
4434 // should be a dark wake or a full wake. Both Maintenance and SleepService
4435 // alarms are dark wake, while AutoWake (WakeByCalendarDate) and DebugWake
4436 // (WakeRelativeToSleep) should trigger a full wake. Scheduled power-on
4437 // PMSettings are ignored.
4438 //
4439 // Caller serialized using settingsCtrlLock.
4440 //******************************************************************************
4441 
4442 void
4443 IOPMrootDomain::recordRTCAlarm(
4444 	const OSSymbol  *type,
4445 	OSObject        *object )
4446 {
4447 	uint32_t previousAlarmMask = _scheduledAlarmMask;
4448 
4449 	if (type == gIOPMSettingDebugWakeRelativeKey) {
4450 		OSNumber * n = OSDynamicCast(OSNumber, object);
4451 		if (n) {
4452 			// Debug wake has highest scheduling priority so it overrides any
4453 			// pre-existing alarm.
4454 			uint32_t debugSecs = n->unsigned32BitValue();
4455 			_nextScheduledAlarmType.reset(type, OSRetain);
4456 			_nextScheduledAlarmUTC = debugSecs;
4457 
4458 			_debugWakeSeconds = debugSecs;
4459 			OSBitOrAtomic(kIOPMAlarmBitDebugWake, &_scheduledAlarmMask);
4460 			DLOG("next alarm (%s) in %u secs\n",
4461 			    type->getCStringNoCopy(), debugSecs);
4462 		}
4463 	} else if ((type == gIOPMSettingAutoWakeCalendarKey.get()) ||
4464 	    (type == gIOPMSettingMaintenanceWakeCalendarKey.get()) ||
4465 	    (type == gIOPMSettingSleepServiceWakeCalendarKey.get())) {
4466 		OSData * data = OSDynamicCast(OSData, object);
4467 		if (data && (data->getLength() == sizeof(IOPMCalendarStruct))) {
4468 			const IOPMCalendarStruct * cs;
4469 			bool replaceNextAlarm = false;
4470 			clock_sec_t secs;
4471 
4472 			cs = (const IOPMCalendarStruct *) data->getBytesNoCopy();
4473 			secs = IOPMConvertCalendarToSeconds(cs);
4474 			DLOG("%s " YMDTF "\n", type->getCStringNoCopy(), YMDT(cs));
4475 
4476 			// Update the next scheduled alarm type
4477 			if ((_nextScheduledAlarmType == NULL) ||
4478 			    ((_nextScheduledAlarmType != gIOPMSettingDebugWakeRelativeKey) &&
4479 			    (secs < _nextScheduledAlarmUTC))) {
4480 				replaceNextAlarm = true;
4481 			}
4482 
4483 			if (type == gIOPMSettingAutoWakeCalendarKey.get()) {
4484 				if (cs->year) {
4485 					_calendarWakeAlarmUTC = IOPMConvertCalendarToSeconds(cs);
4486 					OSBitOrAtomic(kIOPMAlarmBitCalendarWake, &_scheduledAlarmMask);
4487 				} else {
4488 					// TODO: can this else-block be removed?
4489 					_calendarWakeAlarmUTC = 0;
4490 					OSBitAndAtomic(~kIOPMAlarmBitCalendarWake, &_scheduledAlarmMask);
4491 				}
4492 			}
4493 			if (type == gIOPMSettingMaintenanceWakeCalendarKey.get()) {
4494 				OSBitOrAtomic(kIOPMAlarmBitMaintenanceWake, &_scheduledAlarmMask);
4495 			}
4496 			if (type == gIOPMSettingSleepServiceWakeCalendarKey.get()) {
4497 				OSBitOrAtomic(kIOPMAlarmBitSleepServiceWake, &_scheduledAlarmMask);
4498 			}
4499 
4500 			if (replaceNextAlarm) {
4501 				_nextScheduledAlarmType.reset(type, OSRetain);
4502 				_nextScheduledAlarmUTC = secs;
4503 				DLOG("next alarm (%s) " YMDTF "\n", type->getCStringNoCopy(), YMDT(cs));
4504 			}
4505 		}
4506 	}
4507 
4508 	if (_scheduledAlarmMask != previousAlarmMask) {
4509 		DLOG("scheduled alarm mask 0x%x\n", (uint32_t) _scheduledAlarmMask);
4510 	}
4511 }
4512 
4513 // MARK: -
4514 // MARK: Features
4515 
4516 //******************************************************************************
4517 // publishFeature
4518 //
4519 // Adds a new feature to the supported features dictionary
4520 //******************************************************************************
4521 
4522 void
4523 IOPMrootDomain::publishFeature( const char * feature )
4524 {
4525 	publishFeature(feature, kRD_AllPowerSources, NULL);
4526 }
4527 
4528 //******************************************************************************
4529 // publishFeature (with supported power source specified)
4530 //
4531 // Adds a new feature to the supported features dictionary
4532 //******************************************************************************
4533 
4534 void
4535 IOPMrootDomain::publishFeature(
4536 	const char *feature,
4537 	uint32_t supportedWhere,
4538 	uint32_t *uniqueFeatureID)
4539 {
4540 	static uint16_t       next_feature_id = 500;
4541 
4542 	OSSharedPtr<OSNumber> new_feature_data;
4543 	OSNumber             *existing_feature = NULL;
4544 	OSArray              *existing_feature_arr_raw = NULL;
4545 	OSSharedPtr<OSArray>  existing_feature_arr;
4546 	OSObject             *osObj = NULL;
4547 	uint32_t              feature_value = 0;
4548 
4549 	supportedWhere &= kRD_AllPowerSources; // mask off any craziness!
4550 
4551 	if (!supportedWhere) {
4552 		// Feature isn't supported anywhere!
4553 		return;
4554 	}
4555 
4556 	if (next_feature_id > 5000) {
4557 		// Far, far too many features!
4558 		return;
4559 	}
4560 
4561 	if (featuresDictLock) {
4562 		IOLockLock(featuresDictLock);
4563 	}
4564 
4565 	OSSharedPtr<OSObject> origFeaturesProp = copyProperty(kRootDomainSupportedFeatures);
4566 	OSDictionary *origFeatures = OSDynamicCast(OSDictionary, origFeaturesProp.get());
4567 	OSSharedPtr<OSDictionary> features;
4568 
4569 	// Create new features dict if necessary
4570 	if (origFeatures) {
4571 		features = OSDictionary::withDictionary(origFeatures);
4572 	} else {
4573 		features = OSDictionary::withCapacity(1);
4574 	}
4575 
4576 	// Create OSNumber to track new feature
4577 
4578 	next_feature_id += 1;
4579 	if (uniqueFeatureID) {
4580 		// We don't really mind if the calling kext didn't give us a place
4581 		// to stash their unique id. Many kexts don't plan to unload, and thus
4582 		// have no need to remove themselves later.
4583 		*uniqueFeatureID = next_feature_id;
4584 	}
4585 
4586 	feature_value = (uint32_t)next_feature_id;
4587 	feature_value <<= 16;
4588 	feature_value += supportedWhere;
4589 
4590 	new_feature_data = OSNumber::withNumber(
4591 		(unsigned long long)feature_value, 32);
4592 
4593 	// Does features object already exist?
4594 	if ((osObj = features->getObject(feature))) {
4595 		if ((existing_feature = OSDynamicCast(OSNumber, osObj))) {
4596 			// We need to create an OSArray to hold the now 2 elements.
4597 			existing_feature_arr = OSArray::withObjects(
4598 				(const OSObject **)&existing_feature, 1, 2);
4599 		} else if ((existing_feature_arr_raw = OSDynamicCast(OSArray, osObj))) {
4600 			// Add object to existing array
4601 			existing_feature_arr = OSArray::withArray(
4602 				existing_feature_arr_raw,
4603 				existing_feature_arr_raw->getCount() + 1);
4604 		}
4605 
4606 		if (existing_feature_arr) {
4607 			existing_feature_arr->setObject(new_feature_data.get());
4608 			features->setObject(feature, existing_feature_arr.get());
4609 		}
4610 	} else {
4611 		// The easy case: no previously existing features listed. We simply
4612 		// set the OSNumber at key 'feature' and we're on our way.
4613 		features->setObject(feature, new_feature_data.get());
4614 	}
4615 
4616 	setProperty(kRootDomainSupportedFeatures, features.get());
4617 
4618 	if (featuresDictLock) {
4619 		IOLockUnlock(featuresDictLock);
4620 	}
4621 
4622 	// Notify EnergySaver and all those in user space so they might
4623 	// re-populate their feature specific UI
4624 	if (pmPowerStateQueue) {
4625 		pmPowerStateQueue->submitPowerEvent( kPowerEventFeatureChanged );
4626 	}
4627 }
4628 
4629 //******************************************************************************
4630 // removePublishedFeature
4631 //
4632 // Removes previously published feature
4633 //******************************************************************************
4634 
4635 IOReturn
4636 IOPMrootDomain::removePublishedFeature( uint32_t removeFeatureID )
4637 {
4638 	IOReturn                ret = kIOReturnError;
4639 	uint32_t                feature_value = 0;
4640 	uint16_t                feature_id = 0;
4641 	bool                    madeAChange = false;
4642 
4643 	OSSymbol                *dictKey = NULL;
4644 	OSSharedPtr<OSCollectionIterator>    dictIterator;
4645 	OSArray                 *arrayMember  = NULL;
4646 	OSNumber                *numberMember = NULL;
4647 	OSObject                *osObj        = NULL;
4648 	OSNumber                *osNum        = NULL;
4649 	OSSharedPtr<OSArray>    arrayMemberCopy;
4650 
4651 	if (kBadPMFeatureID == removeFeatureID) {
4652 		return kIOReturnNotFound;
4653 	}
4654 
4655 	if (featuresDictLock) {
4656 		IOLockLock(featuresDictLock);
4657 	}
4658 
4659 	OSSharedPtr<OSObject> origFeaturesProp = copyProperty(kRootDomainSupportedFeatures);
4660 	OSDictionary *origFeatures = OSDynamicCast(OSDictionary, origFeaturesProp.get());
4661 	OSSharedPtr<OSDictionary> features;
4662 
4663 	if (origFeatures) {
4664 		// Any modifications to the dictionary are made to the copy to prevent
4665 		// races & crashes with userland clients. Dictionary updated
4666 		// automically later.
4667 		features = OSDictionary::withDictionary(origFeatures);
4668 	} else {
4669 		features = NULL;
4670 		ret = kIOReturnNotFound;
4671 		goto exit;
4672 	}
4673 
4674 	// We iterate 'features' dictionary looking for an entry tagged
4675 	// with 'removeFeatureID'. If found, we remove it from our tracking
4676 	// structures and notify the OS via a general interest message.
4677 
4678 	dictIterator = OSCollectionIterator::withCollection(features.get());
4679 	if (!dictIterator) {
4680 		goto exit;
4681 	}
4682 
4683 	while ((dictKey = OSDynamicCast(OSSymbol, dictIterator->getNextObject()))) {
4684 		osObj = features->getObject(dictKey);
4685 
4686 		// Each Feature is either tracked by an OSNumber
4687 		if (osObj && (numberMember = OSDynamicCast(OSNumber, osObj))) {
4688 			feature_value = numberMember->unsigned32BitValue();
4689 			feature_id = (uint16_t)(feature_value >> 16);
4690 
4691 			if (feature_id == (uint16_t)removeFeatureID) {
4692 				// Remove this node
4693 				features->removeObject(dictKey);
4694 				madeAChange = true;
4695 				break;
4696 			}
4697 
4698 			// Or tracked by an OSArray of OSNumbers
4699 		} else if (osObj && (arrayMember = OSDynamicCast(OSArray, osObj))) {
4700 			unsigned int arrayCount = arrayMember->getCount();
4701 
4702 			for (unsigned int i = 0; i < arrayCount; i++) {
4703 				osNum = OSDynamicCast(OSNumber, arrayMember->getObject(i));
4704 				if (!osNum) {
4705 					continue;
4706 				}
4707 
4708 				feature_value = osNum->unsigned32BitValue();
4709 				feature_id = (uint16_t)(feature_value >> 16);
4710 
4711 				if (feature_id == (uint16_t)removeFeatureID) {
4712 					// Remove this node
4713 					if (1 == arrayCount) {
4714 						// If the array only contains one element, remove
4715 						// the whole thing.
4716 						features->removeObject(dictKey);
4717 					} else {
4718 						// Otherwise remove the element from a copy of the array.
4719 						arrayMemberCopy = OSArray::withArray(arrayMember);
4720 						if (arrayMemberCopy) {
4721 							arrayMemberCopy->removeObject(i);
4722 							features->setObject(dictKey, arrayMemberCopy.get());
4723 						}
4724 					}
4725 
4726 					madeAChange = true;
4727 					break;
4728 				}
4729 			}
4730 		}
4731 	}
4732 
4733 	if (madeAChange) {
4734 		ret = kIOReturnSuccess;
4735 
4736 		setProperty(kRootDomainSupportedFeatures, features.get());
4737 
4738 		// Notify EnergySaver and all those in user space so they might
4739 		// re-populate their feature specific UI
4740 		if (pmPowerStateQueue) {
4741 			pmPowerStateQueue->submitPowerEvent( kPowerEventFeatureChanged );
4742 		}
4743 	} else {
4744 		ret = kIOReturnNotFound;
4745 	}
4746 
4747 exit:
4748 	if (featuresDictLock) {
4749 		IOLockUnlock(featuresDictLock);
4750 	}
4751 	return ret;
4752 }
4753 
4754 //******************************************************************************
4755 // publishPMSetting (private)
4756 //
4757 // Should only be called by PMSettingObject to publish a PM Setting as a
4758 // supported feature.
4759 //******************************************************************************
4760 
4761 void
4762 IOPMrootDomain::publishPMSetting(
4763 	const OSSymbol * feature, uint32_t where, uint32_t * featureID )
4764 {
4765 	if (noPublishPMSettings &&
4766 	    (noPublishPMSettings->getNextIndexOfObject(feature, 0) != (unsigned int)-1)) {
4767 		// Setting found in noPublishPMSettings array
4768 		*featureID = kBadPMFeatureID;
4769 		return;
4770 	}
4771 
4772 	publishFeature(
4773 		feature->getCStringNoCopy(), where, featureID);
4774 }
4775 
4776 //******************************************************************************
4777 // setPMSetting (private)
4778 //
4779 // Internal helper to relay PM settings changes from user space to individual
4780 // drivers. Should be called only by IOPMrootDomain::setProperties.
4781 //******************************************************************************
4782 
4783 IOReturn
4784 IOPMrootDomain::setPMSetting(
4785 	const OSSymbol  *type,
4786 	OSObject        *object )
4787 {
4788 	PMSettingCallEntry  *entries = NULL;
4789 	OSSharedPtr<OSArray>    chosen;
4790 	const OSArray       *array;
4791 	PMSettingObject     *pmso;
4792 	thread_t            thisThread;
4793 	int                 i, j, count, capacity;
4794 	bool                ok = false;
4795 	IOReturn            ret;
4796 
4797 	if (NULL == type) {
4798 		return kIOReturnBadArgument;
4799 	}
4800 
4801 	PMSETTING_LOCK();
4802 
4803 	// Update settings dict so changes are visible from copyPMSetting().
4804 	fPMSettingsDict->setObject(type, object);
4805 
4806 	// Prep all PMSetting objects with the given 'type' for callout.
4807 	array = OSDynamicCast(OSArray, settingsCallbacks->getObject(type));
4808 	if (!array || ((capacity = array->getCount()) == 0)) {
4809 		goto unlock_exit;
4810 	}
4811 
4812 	// Array to retain PMSetting objects targeted for callout.
4813 	chosen = OSArray::withCapacity(capacity);
4814 	if (!chosen) {
4815 		goto unlock_exit; // error
4816 	}
4817 	entries = IONew(PMSettingCallEntry, capacity);
4818 	if (!entries) {
4819 		goto unlock_exit; // error
4820 	}
4821 	memset(entries, 0, sizeof(PMSettingCallEntry) * capacity);
4822 
4823 	thisThread = current_thread();
4824 
4825 	for (i = 0, j = 0; i < capacity; i++) {
4826 		pmso = (PMSettingObject *) array->getObject(i);
4827 		if (pmso->disabled) {
4828 			continue;
4829 		}
4830 		entries[j].thread = thisThread;
4831 		queue_enter(&pmso->calloutQueue, &entries[j], PMSettingCallEntry *, link);
4832 		chosen->setObject(pmso);
4833 		j++;
4834 	}
4835 	count = j;
4836 	if (!count) {
4837 		goto unlock_exit;
4838 	}
4839 
4840 	PMSETTING_UNLOCK();
4841 
4842 	// Call each pmso in the chosen array.
4843 	for (i = 0; i < count; i++) {
4844 		pmso = (PMSettingObject *) chosen->getObject(i);
4845 		ret = pmso->dispatchPMSetting(type, object);
4846 		if (ret == kIOReturnSuccess) {
4847 			// At least one setting handler was successful
4848 			ok = true;
4849 #if DEVELOPMENT || DEBUG
4850 		} else {
4851 			// Log the handler and kext that failed
4852 			OSSharedPtr<const OSSymbol> kextName = copyKextIdentifierWithAddress((vm_address_t) pmso->func);
4853 			if (kextName) {
4854 				DLOG("PMSetting(%s) error 0x%x from %s\n",
4855 				    type->getCStringNoCopy(), ret, kextName->getCStringNoCopy());
4856 			}
4857 #endif
4858 		}
4859 	}
4860 
4861 	PMSETTING_LOCK();
4862 	for (i = 0; i < count; i++) {
4863 		pmso = (PMSettingObject *) chosen->getObject(i);
4864 		queue_remove(&pmso->calloutQueue, &entries[i], PMSettingCallEntry *, link);
4865 		if (pmso->waitThread) {
4866 			PMSETTING_WAKEUP(pmso);
4867 		}
4868 	}
4869 
4870 	if (ok) {
4871 		recordRTCAlarm(type, object);
4872 	}
4873 unlock_exit:
4874 	PMSETTING_UNLOCK();
4875 
4876 	if (entries) {
4877 		IODelete(entries, PMSettingCallEntry, capacity);
4878 	}
4879 
4880 	return kIOReturnSuccess;
4881 }
4882 
4883 //******************************************************************************
4884 // copyPMSetting (public)
4885 //
4886 // Allows kexts to safely read setting values, without being subscribed to
4887 // notifications.
4888 //******************************************************************************
4889 
4890 OSSharedPtr<OSObject>
4891 IOPMrootDomain::copyPMSetting(
4892 	OSSymbol *whichSetting)
4893 {
4894 	OSSharedPtr<OSObject> obj;
4895 
4896 	if (!whichSetting) {
4897 		return NULL;
4898 	}
4899 
4900 	PMSETTING_LOCK();
4901 	obj.reset(fPMSettingsDict->getObject(whichSetting), OSRetain);
4902 	PMSETTING_UNLOCK();
4903 
4904 	return obj;
4905 }
4906 
4907 //******************************************************************************
4908 // registerPMSettingController (public)
4909 //
4910 // direct wrapper to registerPMSettingController with uint32_t power source arg
4911 //******************************************************************************
4912 
4913 IOReturn
4914 IOPMrootDomain::registerPMSettingController(
4915 	const OSSymbol *                settings[],
4916 	IOPMSettingControllerCallback   func,
4917 	OSObject                        *target,
4918 	uintptr_t                       refcon,
4919 	OSObject                        **handle)
4920 {
4921 	return registerPMSettingController(
4922 		settings,
4923 		(kIOPMSupportedOnAC | kIOPMSupportedOnBatt | kIOPMSupportedOnUPS),
4924 		func, target, refcon, handle);
4925 }
4926 
4927 //******************************************************************************
4928 // registerPMSettingController (public)
4929 //
4930 // Kexts may register for notifications when a particular setting is changed.
4931 // A list of settings is available in IOPM.h.
4932 // Arguments:
4933 //  * settings - An OSArray containing OSSymbols. Caller should populate this
4934 //          array with a list of settings caller wants notifications from.
4935 //  * func - A C function callback of the type IOPMSettingControllerCallback
4936 //  * target - caller may provide an OSObject *, which PM will pass as an
4937 //          target to calls to "func"
4938 //  * refcon - caller may provide an void *, which PM will pass as an
4939 //          argument to calls to "func"
4940 //  * handle - This is a return argument. We will populate this pointer upon
4941 //          call success. Hold onto this and pass this argument to
4942 //          IOPMrootDomain::deRegisterPMSettingCallback when unloading your kext
4943 // Returns:
4944 //      kIOReturnSuccess on success
4945 //******************************************************************************
4946 
4947 IOReturn
4948 IOPMrootDomain::registerPMSettingController(
4949 	const OSSymbol *                settings[],
4950 	uint32_t                        supportedPowerSources,
4951 	IOPMSettingControllerCallback   func,
4952 	OSObject                        *target,
4953 	uintptr_t                       refcon,
4954 	OSObject                        **handle)
4955 {
4956 	PMSettingObject *pmso = NULL;
4957 	OSObject        *pmsh = NULL;
4958 	int             i;
4959 
4960 	if (NULL == settings ||
4961 	    NULL == func ||
4962 	    NULL == handle) {
4963 		return kIOReturnBadArgument;
4964 	}
4965 
4966 	pmso = PMSettingObject::pmSettingObject(
4967 		(IOPMrootDomain *) this, func, target,
4968 		refcon, supportedPowerSources, settings, &pmsh);
4969 
4970 	if (!pmso) {
4971 		*handle = NULL;
4972 		return kIOReturnInternalError;
4973 	}
4974 
4975 	PMSETTING_LOCK();
4976 	for (i = 0; settings[i]; i++) {
4977 		OSSharedPtr<OSArray> newList;
4978 		OSArray *list = OSDynamicCast(OSArray, settingsCallbacks->getObject(settings[i]));
4979 		if (!list) {
4980 			// New array of callbacks for this setting
4981 			newList = OSArray::withCapacity(1);
4982 			settingsCallbacks->setObject(settings[i], newList.get());
4983 			list = newList.get();
4984 		}
4985 
4986 		// Add caller to the callback list
4987 		list->setObject(pmso);
4988 	}
4989 	PMSETTING_UNLOCK();
4990 
4991 	// Return handle to the caller, the setting object is private.
4992 	*handle = pmsh;
4993 
4994 	return kIOReturnSuccess;
4995 }
4996 
4997 //******************************************************************************
4998 // deregisterPMSettingObject (private)
4999 //
5000 // Only called from PMSettingObject.
5001 //******************************************************************************
5002 
5003 void
5004 IOPMrootDomain::deregisterPMSettingObject( PMSettingObject * pmso )
5005 {
5006 	thread_t                thisThread = current_thread();
5007 	PMSettingCallEntry      *callEntry;
5008 	OSSharedPtr<OSCollectionIterator>    iter;
5009 	OSSymbol                *sym;
5010 	OSArray                 *array;
5011 	int                     index;
5012 	bool                    wait;
5013 
5014 	PMSETTING_LOCK();
5015 
5016 	pmso->disabled = true;
5017 
5018 	// Wait for all callout threads to finish.
5019 	do {
5020 		wait = false;
5021 		queue_iterate(&pmso->calloutQueue, callEntry, PMSettingCallEntry *, link)
5022 		{
5023 			if (callEntry->thread != thisThread) {
5024 				wait = true;
5025 				break;
5026 			}
5027 		}
5028 		if (wait) {
5029 			assert(NULL == pmso->waitThread);
5030 			pmso->waitThread = thisThread;
5031 			PMSETTING_WAIT(pmso);
5032 			pmso->waitThread = NULL;
5033 		}
5034 	} while (wait);
5035 
5036 	// Search each PM settings array in the kernel.
5037 	iter = OSCollectionIterator::withCollection(settingsCallbacks.get());
5038 	if (iter) {
5039 		while ((sym = OSDynamicCast(OSSymbol, iter->getNextObject()))) {
5040 			array = OSDynamicCast(OSArray, settingsCallbacks->getObject(sym));
5041 			index = array->getNextIndexOfObject(pmso, 0);
5042 			if (-1 != index) {
5043 				array->removeObject(index);
5044 			}
5045 		}
5046 	}
5047 
5048 	PMSETTING_UNLOCK();
5049 
5050 	pmso->release();
5051 }
5052 
5053 //******************************************************************************
5054 // informCPUStateChange
5055 //
5056 // Call into PM CPU code so that CPU power savings may dynamically adjust for
5057 // running on battery, with the lid closed, etc.
5058 //
5059 // informCPUStateChange is a no-op on non x86 systems
5060 // only x86 has explicit support in the IntelCPUPowerManagement kext
5061 //******************************************************************************
5062 
5063 void
5064 IOPMrootDomain::informCPUStateChange(
5065 	uint32_t type,
5066 	uint32_t value )
5067 {
5068 #if defined(__i386__) || defined(__x86_64__)
5069 
5070 	pmioctlVariableInfo_t varInfoStruct;
5071 	int                 pmCPUret = 0;
5072 	const char          *varNameStr = NULL;
5073 	int32_t             *varIndex   = NULL;
5074 
5075 	if (kInformAC == type) {
5076 		varNameStr = kIOPMRootDomainBatPowerCString;
5077 		varIndex = &idxPMCPULimitedPower;
5078 	} else if (kInformLid == type) {
5079 		varNameStr = kIOPMRootDomainLidCloseCString;
5080 		varIndex = &idxPMCPUClamshell;
5081 	} else {
5082 		return;
5083 	}
5084 
5085 	// Set the new value!
5086 	// pmCPUControl will assign us a new ID if one doesn't exist yet
5087 	bzero(&varInfoStruct, sizeof(pmioctlVariableInfo_t));
5088 	varInfoStruct.varID         = *varIndex;
5089 	varInfoStruct.varType       = vBool;
5090 	varInfoStruct.varInitValue  = value;
5091 	varInfoStruct.varCurValue   = value;
5092 	strlcpy((char *)varInfoStruct.varName,
5093 	    (const char *)varNameStr,
5094 	    sizeof(varInfoStruct.varName));
5095 
5096 	// Set!
5097 	pmCPUret = pmCPUControl( PMIOCSETVARINFO, (void *)&varInfoStruct );
5098 
5099 	// pmCPU only assigns numerical id's when a new varName is specified
5100 	if ((0 == pmCPUret)
5101 	    && (*varIndex == kCPUUnknownIndex)) {
5102 		// pmCPUControl has assigned us a new variable ID.
5103 		// Let's re-read the structure we just SET to learn that ID.
5104 		pmCPUret = pmCPUControl( PMIOCGETVARNAMEINFO, (void *)&varInfoStruct );
5105 
5106 		if (0 == pmCPUret) {
5107 			// Store it in idxPMCPUClamshell or idxPMCPULimitedPower
5108 			*varIndex = varInfoStruct.varID;
5109 		}
5110 	}
5111 
5112 	return;
5113 
5114 #endif /* __i386__ || __x86_64__ */
5115 }
5116 
5117 // MARK: -
5118 // MARK: Deep Sleep Policy
5119 
5120 #if HIBERNATION
5121 
5122 //******************************************************************************
5123 // evaluateSystemSleepPolicy
5124 //******************************************************************************
5125 
5126 #define kIOPlatformSystemSleepPolicyKey     "IOPlatformSystemSleepPolicy"
5127 
5128 // Sleep flags
5129 enum {
5130 	kIOPMSleepFlagHibernate         = 0x00000001,
5131 	kIOPMSleepFlagSleepTimerEnable  = 0x00000002
5132 };
5133 
5134 struct IOPMSystemSleepPolicyEntry {
5135 	uint32_t    factorMask;
5136 	uint32_t    factorBits;
5137 	uint32_t    sleepFlags;
5138 	uint32_t    wakeEvents;
5139 } __attribute__((packed));
5140 
5141 struct IOPMSystemSleepPolicyTable {
5142 	uint32_t    signature;
5143 	uint16_t    version;
5144 	uint16_t    entryCount;
5145 	IOPMSystemSleepPolicyEntry  entries[];
5146 } __attribute__((packed));
5147 
5148 enum {
5149 	kIOPMSleepAttributeHibernateSetup   = 0x00000001,
5150 	kIOPMSleepAttributeHibernateSleep   = 0x00000002
5151 };
5152 
5153 static uint32_t
5154 getSleepTypeAttributes( uint32_t sleepType )
5155 {
5156 	static const uint32_t sleepTypeAttributes[kIOPMSleepTypeLast] =
5157 	{
5158 		/* invalid   */ 0,
5159 		/* abort     */ 0,
5160 		/* normal    */ 0,
5161 		/* safesleep */ kIOPMSleepAttributeHibernateSetup,
5162 		/* hibernate */ kIOPMSleepAttributeHibernateSetup | kIOPMSleepAttributeHibernateSleep,
5163 		/* standby   */ kIOPMSleepAttributeHibernateSetup | kIOPMSleepAttributeHibernateSleep,
5164 		/* poweroff  */ kIOPMSleepAttributeHibernateSetup | kIOPMSleepAttributeHibernateSleep,
5165 		/* deepidle  */ 0
5166 	};
5167 
5168 	if (sleepType >= kIOPMSleepTypeLast) {
5169 		return 0;
5170 	}
5171 
5172 	return sleepTypeAttributes[sleepType];
5173 }
5174 
5175 bool
5176 IOPMrootDomain::evaluateSystemSleepPolicy(
5177 	IOPMSystemSleepParameters * params, int sleepPhase, uint32_t * hibMode )
5178 {
5179 #define SLEEP_FACTOR(x) {(uint32_t) kIOPMSleepFactor ## x, #x}
5180 
5181 	static const IONamedValue factorValues[] = {
5182 		SLEEP_FACTOR( SleepTimerWake ),
5183 		SLEEP_FACTOR( LidOpen ),
5184 		SLEEP_FACTOR( ACPower ),
5185 		SLEEP_FACTOR( BatteryLow ),
5186 		SLEEP_FACTOR( StandbyNoDelay ),
5187 		SLEEP_FACTOR( StandbyForced ),
5188 		SLEEP_FACTOR( StandbyDisabled ),
5189 		SLEEP_FACTOR( USBExternalDevice ),
5190 		SLEEP_FACTOR( BluetoothHIDDevice ),
5191 		SLEEP_FACTOR( ExternalMediaMounted ),
5192 		SLEEP_FACTOR( ThunderboltDevice ),
5193 		SLEEP_FACTOR( RTCAlarmScheduled ),
5194 		SLEEP_FACTOR( MagicPacketWakeEnabled ),
5195 		SLEEP_FACTOR( HibernateForced ),
5196 		SLEEP_FACTOR( AutoPowerOffDisabled ),
5197 		SLEEP_FACTOR( AutoPowerOffForced ),
5198 		SLEEP_FACTOR( ExternalDisplay ),
5199 		SLEEP_FACTOR( NetworkKeepAliveActive ),
5200 		SLEEP_FACTOR( LocalUserActivity ),
5201 		SLEEP_FACTOR( HibernateFailed ),
5202 		SLEEP_FACTOR( ThermalWarning ),
5203 		SLEEP_FACTOR( DisplayCaptured ),
5204 		{ 0, NULL }
5205 	};
5206 
5207 	const IOPMSystemSleepPolicyTable * pt;
5208 	OSSharedPtr<OSObject>  prop;
5209 	OSData *    policyData;
5210 	uint64_t    currentFactors = 0;
5211 	char        currentFactorsBuf[512];
5212 	uint32_t    standbyDelay   = 0;
5213 	uint32_t    powerOffDelay  = 0;
5214 	uint32_t    powerOffTimer  = 0;
5215 	uint32_t    standbyTimer  = 0;
5216 	uint32_t    mismatch;
5217 	bool        standbyEnabled;
5218 	bool        powerOffEnabled;
5219 	bool        found = false;
5220 
5221 	// Get platform's sleep policy table
5222 	if (!gSleepPolicyHandler) {
5223 		prop = getServiceRoot()->copyProperty(kIOPlatformSystemSleepPolicyKey);
5224 		if (!prop) {
5225 			goto done;
5226 		}
5227 	}
5228 
5229 	// Fetch additional settings
5230 	standbyEnabled = (getSleepOption(kIOPMDeepSleepDelayKey, &standbyDelay)
5231 	    && propertyHasValue(kIOPMDeepSleepEnabledKey, kOSBooleanTrue));
5232 	powerOffEnabled = (getSleepOption(kIOPMAutoPowerOffDelayKey, &powerOffDelay)
5233 	    && propertyHasValue(kIOPMAutoPowerOffEnabledKey, kOSBooleanTrue));
5234 	if (!getSleepOption(kIOPMAutoPowerOffTimerKey, &powerOffTimer)) {
5235 		powerOffTimer = powerOffDelay;
5236 	}
5237 	if (!getSleepOption(kIOPMDeepSleepTimerKey, &standbyTimer)) {
5238 		standbyTimer = standbyDelay;
5239 	}
5240 
5241 	DLOG("phase %d, standby %d delay %u timer %u, poweroff %d delay %u timer %u, hibernate 0x%x\n",
5242 	    sleepPhase, standbyEnabled, standbyDelay, standbyTimer,
5243 	    powerOffEnabled, powerOffDelay, powerOffTimer, *hibMode);
5244 
5245 	currentFactorsBuf[0] = 0;
5246 	// pmset level overrides
5247 	if ((*hibMode & kIOHibernateModeOn) == 0) {
5248 		if (!gSleepPolicyHandler) {
5249 			standbyEnabled  = false;
5250 			powerOffEnabled = false;
5251 		}
5252 	} else if (!(*hibMode & kIOHibernateModeSleep)) {
5253 		// Force hibernate (i.e. mode 25)
5254 		// If standby is enabled, force standy.
5255 		// If poweroff is enabled, force poweroff.
5256 		if (standbyEnabled) {
5257 			currentFactors |= kIOPMSleepFactorStandbyForced;
5258 		} else if (powerOffEnabled) {
5259 			currentFactors |= kIOPMSleepFactorAutoPowerOffForced;
5260 		} else {
5261 			currentFactors |= kIOPMSleepFactorHibernateForced;
5262 		}
5263 	}
5264 
5265 	// Current factors based on environment and assertions
5266 	if (sleepTimerMaintenance) {
5267 		currentFactors |= kIOPMSleepFactorSleepTimerWake;
5268 	}
5269 	if (standbyEnabled && sleepToStandby && !gSleepPolicyHandler) {
5270 		currentFactors |= kIOPMSleepFactorSleepTimerWake;
5271 	}
5272 	if (!clamshellClosed) {
5273 		currentFactors |= kIOPMSleepFactorLidOpen;
5274 	}
5275 	if (acAdaptorConnected) {
5276 		currentFactors |= kIOPMSleepFactorACPower;
5277 	}
5278 	if (lowBatteryCondition) {
5279 		hibernateMode = 0;
5280 		getSleepOption(kIOHibernateModeKey, &hibernateMode);
5281 		if ((hibernateMode & kIOHibernateModeOn) == 0) {
5282 			DLOG("HibernateMode is 0. Not sending LowBattery factor to IOPPF\n");
5283 		} else {
5284 			currentFactors |= kIOPMSleepFactorBatteryLow;
5285 		}
5286 	}
5287 	if (!standbyDelay || !standbyTimer) {
5288 		currentFactors |= kIOPMSleepFactorStandbyNoDelay;
5289 	}
5290 	if (standbyNixed || !standbyEnabled) {
5291 		currentFactors |= kIOPMSleepFactorStandbyDisabled;
5292 	}
5293 	if (resetTimers) {
5294 		currentFactors |= kIOPMSleepFactorLocalUserActivity;
5295 		currentFactors &= ~kIOPMSleepFactorSleepTimerWake;
5296 	}
5297 	if (getPMAssertionLevel(kIOPMDriverAssertionUSBExternalDeviceBit) !=
5298 	    kIOPMDriverAssertionLevelOff) {
5299 		currentFactors |= kIOPMSleepFactorUSBExternalDevice;
5300 	}
5301 	if (getPMAssertionLevel(kIOPMDriverAssertionBluetoothHIDDevicePairedBit) !=
5302 	    kIOPMDriverAssertionLevelOff) {
5303 		currentFactors |= kIOPMSleepFactorBluetoothHIDDevice;
5304 	}
5305 	if (getPMAssertionLevel(kIOPMDriverAssertionExternalMediaMountedBit) !=
5306 	    kIOPMDriverAssertionLevelOff) {
5307 		currentFactors |= kIOPMSleepFactorExternalMediaMounted;
5308 	}
5309 	if (getPMAssertionLevel(kIOPMDriverAssertionReservedBit5) !=
5310 	    kIOPMDriverAssertionLevelOff) {
5311 		currentFactors |= kIOPMSleepFactorThunderboltDevice;
5312 	}
5313 	if (_scheduledAlarmMask != 0) {
5314 		currentFactors |= kIOPMSleepFactorRTCAlarmScheduled;
5315 	}
5316 	if (getPMAssertionLevel(kIOPMDriverAssertionMagicPacketWakeEnabledBit) !=
5317 	    kIOPMDriverAssertionLevelOff) {
5318 		currentFactors |= kIOPMSleepFactorMagicPacketWakeEnabled;
5319 	}
5320 #define TCPKEEPALIVE 1
5321 #if TCPKEEPALIVE
5322 	if (getPMAssertionLevel(kIOPMDriverAssertionNetworkKeepAliveActiveBit) !=
5323 	    kIOPMDriverAssertionLevelOff) {
5324 		currentFactors |= kIOPMSleepFactorNetworkKeepAliveActive;
5325 	}
5326 #endif
5327 	if (!powerOffEnabled) {
5328 		currentFactors |= kIOPMSleepFactorAutoPowerOffDisabled;
5329 	}
5330 	if (desktopMode) {
5331 		currentFactors |= kIOPMSleepFactorExternalDisplay;
5332 	}
5333 	if (userWasActive) {
5334 		currentFactors |= kIOPMSleepFactorLocalUserActivity;
5335 	}
5336 	if (darkWakeHibernateError && !CAP_HIGHEST(kIOPMSystemCapabilityGraphics)) {
5337 		currentFactors |= kIOPMSleepFactorHibernateFailed;
5338 	}
5339 	if (thermalWarningState) {
5340 		currentFactors |= kIOPMSleepFactorThermalWarning;
5341 	}
5342 
5343 	for (int factorBit = 0; factorBit < (8 * sizeof(uint32_t)); factorBit++) {
5344 		uint32_t factor = 1 << factorBit;
5345 		if (factor & currentFactors) {
5346 			strlcat(currentFactorsBuf, ", ", sizeof(currentFactorsBuf));
5347 			strlcat(currentFactorsBuf, IOFindNameForValue(factor, factorValues), sizeof(currentFactorsBuf));
5348 		}
5349 	}
5350 	DLOG("sleep factors 0x%llx%s\n", currentFactors, currentFactorsBuf);
5351 
5352 	if (gSleepPolicyHandler) {
5353 		uint32_t    savedHibernateMode;
5354 		IOReturn    result;
5355 
5356 		if (!gSleepPolicyVars) {
5357 			gSleepPolicyVars = IOMallocType(IOPMSystemSleepPolicyVariables);
5358 		}
5359 		gSleepPolicyVars->signature = kIOPMSystemSleepPolicySignature;
5360 		gSleepPolicyVars->version   = kIOPMSystemSleepPolicyVersion;
5361 		gSleepPolicyVars->currentCapability = _currentCapability;
5362 		gSleepPolicyVars->highestCapability = _highestCapability;
5363 		gSleepPolicyVars->sleepFactors      = currentFactors;
5364 		gSleepPolicyVars->sleepReason       = lastSleepReason;
5365 		gSleepPolicyVars->sleepPhase        = sleepPhase;
5366 		gSleepPolicyVars->standbyDelay      = standbyDelay;
5367 		gSleepPolicyVars->standbyTimer      = standbyTimer;
5368 		gSleepPolicyVars->poweroffDelay     = powerOffDelay;
5369 		gSleepPolicyVars->scheduledAlarms   = _scheduledAlarmMask | _userScheduledAlarmMask;
5370 		gSleepPolicyVars->poweroffTimer     = powerOffTimer;
5371 
5372 		if (kIOPMSleepPhase0 == sleepPhase) {
5373 			// preserve hibernateMode
5374 			savedHibernateMode = gSleepPolicyVars->hibernateMode;
5375 			gSleepPolicyVars->hibernateMode = *hibMode;
5376 		} else if (kIOPMSleepPhase1 == sleepPhase) {
5377 			// use original hibernateMode for phase2
5378 			gSleepPolicyVars->hibernateMode = *hibMode;
5379 		}
5380 
5381 		result = gSleepPolicyHandler(gSleepPolicyTarget, gSleepPolicyVars, params);
5382 
5383 		if (kIOPMSleepPhase0 == sleepPhase) {
5384 			// restore hibernateMode
5385 			gSleepPolicyVars->hibernateMode = savedHibernateMode;
5386 		}
5387 
5388 		if ((result != kIOReturnSuccess) ||
5389 		    (kIOPMSleepTypeInvalid == params->sleepType) ||
5390 		    (params->sleepType >= kIOPMSleepTypeLast) ||
5391 		    (kIOPMSystemSleepParametersVersion != params->version)) {
5392 			MSG("sleep policy handler error\n");
5393 			goto done;
5394 		}
5395 
5396 		if ((getSleepTypeAttributes(params->sleepType) &
5397 		    kIOPMSleepAttributeHibernateSetup) &&
5398 		    ((*hibMode & kIOHibernateModeOn) == 0)) {
5399 			*hibMode |= (kIOHibernateModeOn | kIOHibernateModeSleep);
5400 		}
5401 
5402 		DLOG("sleep params v%u, type %u, flags 0x%x, wake 0x%x, timer %u, poweroff %u\n",
5403 		    params->version, params->sleepType, params->sleepFlags,
5404 		    params->ecWakeEvents, params->ecWakeTimer, params->ecPoweroffTimer);
5405 		found = true;
5406 		goto done;
5407 	}
5408 
5409 	// Policy table is meaningless without standby enabled
5410 	if (!standbyEnabled) {
5411 		goto done;
5412 	}
5413 
5414 	// Validate the sleep policy table
5415 	policyData = OSDynamicCast(OSData, prop.get());
5416 	if (!policyData || (policyData->getLength() <= sizeof(IOPMSystemSleepPolicyTable))) {
5417 		goto done;
5418 	}
5419 
5420 	pt = (const IOPMSystemSleepPolicyTable *) policyData->getBytesNoCopy();
5421 	if ((pt->signature != kIOPMSystemSleepPolicySignature) ||
5422 	    (pt->version != 1) || (0 == pt->entryCount)) {
5423 		goto done;
5424 	}
5425 
5426 	if (((policyData->getLength() - sizeof(IOPMSystemSleepPolicyTable)) !=
5427 	    (sizeof(IOPMSystemSleepPolicyEntry) * pt->entryCount))) {
5428 		goto done;
5429 	}
5430 
5431 	for (uint32_t i = 0; i < pt->entryCount; i++) {
5432 		const IOPMSystemSleepPolicyEntry * entry = &pt->entries[i];
5433 		mismatch = (((uint32_t)currentFactors ^ entry->factorBits) & entry->factorMask);
5434 
5435 		DLOG("mask 0x%08x, bits 0x%08x, flags 0x%08x, wake 0x%08x, mismatch 0x%08x\n",
5436 		    entry->factorMask, entry->factorBits,
5437 		    entry->sleepFlags, entry->wakeEvents, mismatch);
5438 		if (mismatch) {
5439 			continue;
5440 		}
5441 
5442 		DLOG("^ found match\n");
5443 		found = true;
5444 
5445 		params->version = kIOPMSystemSleepParametersVersion;
5446 		params->reserved1 = 1;
5447 		if (entry->sleepFlags & kIOPMSleepFlagHibernate) {
5448 			params->sleepType = kIOPMSleepTypeStandby;
5449 		} else {
5450 			params->sleepType = kIOPMSleepTypeNormalSleep;
5451 		}
5452 
5453 		params->ecWakeEvents = entry->wakeEvents;
5454 		if (entry->sleepFlags & kIOPMSleepFlagSleepTimerEnable) {
5455 			if (kIOPMSleepPhase2 == sleepPhase) {
5456 				clock_sec_t now_secs = gIOLastSleepTime.tv_sec;
5457 
5458 				if (!_standbyTimerResetSeconds ||
5459 				    (now_secs <= _standbyTimerResetSeconds)) {
5460 					// Reset standby timer adjustment
5461 					_standbyTimerResetSeconds = now_secs;
5462 					DLOG("standby delay %u, reset %u\n",
5463 					    standbyDelay, (uint32_t) _standbyTimerResetSeconds);
5464 				} else if (standbyDelay) {
5465 					// Shorten the standby delay timer
5466 					clock_sec_t elapsed = now_secs - _standbyTimerResetSeconds;
5467 					if (standbyDelay > elapsed) {
5468 						standbyDelay -= elapsed;
5469 					} else {
5470 						standbyDelay = 1; // must be > 0
5471 					}
5472 					DLOG("standby delay %u, elapsed %u\n",
5473 					    standbyDelay, (uint32_t) elapsed);
5474 				}
5475 			}
5476 			params->ecWakeTimer = standbyDelay;
5477 		} else if (kIOPMSleepPhase2 == sleepPhase) {
5478 			// A sleep that does not enable the sleep timer will reset
5479 			// the standby delay adjustment.
5480 			_standbyTimerResetSeconds = 0;
5481 		}
5482 		break;
5483 	}
5484 
5485 done:
5486 	return found;
5487 }
5488 
5489 static IOPMSystemSleepParameters gEarlySystemSleepParams;
5490 
5491 void
5492 IOPMrootDomain::evaluateSystemSleepPolicyEarly( void )
5493 {
5494 	// Evaluate early (priority interest phase), before drivers sleep.
5495 
5496 	DLOG("%s\n", __FUNCTION__);
5497 	removeProperty(kIOPMSystemSleepParametersKey);
5498 
5499 	// Full wake resets the standby timer delay adjustment
5500 	if (_highestCapability & kIOPMSystemCapabilityGraphics) {
5501 		_standbyTimerResetSeconds = 0;
5502 	}
5503 
5504 	hibernateDisabled = false;
5505 	hibernateMode = 0;
5506 	getSleepOption(kIOHibernateModeKey, &hibernateMode);
5507 
5508 	// Save for late evaluation if sleep is aborted
5509 	bzero(&gEarlySystemSleepParams, sizeof(gEarlySystemSleepParams));
5510 
5511 	if (evaluateSystemSleepPolicy(&gEarlySystemSleepParams, kIOPMSleepPhase1,
5512 	    &hibernateMode)) {
5513 		if (!hibernateRetry &&
5514 		    ((getSleepTypeAttributes(gEarlySystemSleepParams.sleepType) &
5515 		    kIOPMSleepAttributeHibernateSetup) == 0)) {
5516 			// skip hibernate setup
5517 			hibernateDisabled = true;
5518 		}
5519 	}
5520 
5521 	// Publish IOPMSystemSleepType
5522 	uint32_t sleepType = gEarlySystemSleepParams.sleepType;
5523 	if (sleepType == kIOPMSleepTypeInvalid) {
5524 		// no sleep policy
5525 		sleepType = kIOPMSleepTypeNormalSleep;
5526 		if (hibernateMode & kIOHibernateModeOn) {
5527 			sleepType = (hibernateMode & kIOHibernateModeSleep) ?
5528 			    kIOPMSleepTypeSafeSleep : kIOPMSleepTypeHibernate;
5529 		}
5530 	} else if ((sleepType == kIOPMSleepTypeStandby) &&
5531 	    (gEarlySystemSleepParams.ecPoweroffTimer)) {
5532 		// report the lowest possible sleep state
5533 		sleepType = kIOPMSleepTypePowerOff;
5534 	}
5535 
5536 	setProperty(kIOPMSystemSleepTypeKey, sleepType, 32);
5537 }
5538 
5539 void
5540 IOPMrootDomain::evaluateSystemSleepPolicyFinal( void )
5541 {
5542 	IOPMSystemSleepParameters   params;
5543 	OSSharedPtr<OSData>         paramsData;
5544 	bool                        wakeNow;
5545 	// Evaluate sleep policy after sleeping drivers but before platform sleep.
5546 
5547 	DLOG("%s\n", __FUNCTION__);
5548 
5549 	bzero(&params, sizeof(params));
5550 	wakeNow = false;
5551 	if (evaluateSystemSleepPolicy(&params, kIOPMSleepPhase2, &hibernateMode)) {
5552 		if ((kIOPMSleepTypeStandby == params.sleepType)
5553 		    && gIOHibernateStandbyDisabled && gSleepPolicyVars
5554 		    && (!((kIOPMSleepFactorStandbyForced | kIOPMSleepFactorAutoPowerOffForced | kIOPMSleepFactorHibernateForced)
5555 		    & gSleepPolicyVars->sleepFactors))) {
5556 			standbyNixed = true;
5557 			wakeNow = true;
5558 		}
5559 		if (wakeNow
5560 		    || ((hibernateDisabled || hibernateAborted) &&
5561 		    (getSleepTypeAttributes(params.sleepType) &
5562 		    kIOPMSleepAttributeHibernateSetup))) {
5563 			// Final evaluation picked a state requiring hibernation,
5564 			// but hibernate isn't going to proceed. Arm a short sleep using
5565 			// the early non-hibernate sleep parameters.
5566 			bcopy(&gEarlySystemSleepParams, &params, sizeof(params));
5567 			params.sleepType = kIOPMSleepTypeAbortedSleep;
5568 			params.ecWakeTimer = 1;
5569 			if (standbyNixed) {
5570 				resetTimers = true;
5571 			} else {
5572 				// Set hibernateRetry flag to force hibernate setup on the
5573 				// next sleep.
5574 				hibernateRetry = true;
5575 			}
5576 			DLOG("wake in %u secs for hibernateDisabled %d, hibernateAborted %d, standbyNixed %d\n",
5577 			    params.ecWakeTimer, hibernateDisabled, hibernateAborted, standbyNixed);
5578 		} else {
5579 			hibernateRetry = false;
5580 		}
5581 
5582 		if (kIOPMSleepTypeAbortedSleep != params.sleepType) {
5583 			resetTimers = false;
5584 		}
5585 
5586 		paramsData = OSData::withValue(params);
5587 		if (paramsData) {
5588 			setProperty(kIOPMSystemSleepParametersKey, paramsData.get());
5589 		}
5590 
5591 		if (getSleepTypeAttributes(params.sleepType) &
5592 		    kIOPMSleepAttributeHibernateSleep) {
5593 			// Disable sleep to force hibernation
5594 			gIOHibernateMode &= ~kIOHibernateModeSleep;
5595 		}
5596 	}
5597 }
5598 
5599 bool
5600 IOPMrootDomain::getHibernateSettings(
5601 	uint32_t *  hibernateModePtr,
5602 	uint32_t *  hibernateFreeRatio,
5603 	uint32_t *  hibernateFreeTime )
5604 {
5605 	// Called by IOHibernateSystemSleep() after evaluateSystemSleepPolicyEarly()
5606 	// has updated the hibernateDisabled flag.
5607 
5608 	bool ok = getSleepOption(kIOHibernateModeKey, hibernateModePtr);
5609 	getSleepOption(kIOHibernateFreeRatioKey, hibernateFreeRatio);
5610 	getSleepOption(kIOHibernateFreeTimeKey, hibernateFreeTime);
5611 	if (hibernateDisabled) {
5612 		*hibernateModePtr = 0;
5613 	} else if (gSleepPolicyHandler) {
5614 		*hibernateModePtr = hibernateMode;
5615 	}
5616 	DLOG("hibernateMode 0x%x\n", *hibernateModePtr);
5617 	return ok;
5618 }
5619 
5620 bool
5621 IOPMrootDomain::getSleepOption( const char * key, uint32_t * option )
5622 {
5623 	OSSharedPtr<OSObject>       optionsProp;
5624 	OSDictionary *              optionsDict;
5625 	OSSharedPtr<OSObject>       obj;
5626 	OSNumber *                  num;
5627 	bool                        ok = false;
5628 
5629 	optionsProp = copyProperty(kRootDomainSleepOptionsKey);
5630 	optionsDict = OSDynamicCast(OSDictionary, optionsProp.get());
5631 
5632 	if (optionsDict) {
5633 		obj.reset(optionsDict->getObject(key), OSRetain);
5634 	}
5635 	if (!obj) {
5636 		obj = copyProperty(key);
5637 	}
5638 	if (obj) {
5639 		if ((num = OSDynamicCast(OSNumber, obj.get()))) {
5640 			*option = num->unsigned32BitValue();
5641 			ok = true;
5642 		} else if (OSDynamicCast(OSBoolean, obj.get())) {
5643 			*option = (obj == kOSBooleanTrue) ? 1 : 0;
5644 			ok = true;
5645 		}
5646 	}
5647 
5648 	return ok;
5649 }
5650 #endif /* HIBERNATION */
5651 
5652 IOReturn
5653 IOPMrootDomain::getSystemSleepType( uint32_t * sleepType, uint32_t * standbyTimer )
5654 {
5655 #if HIBERNATION
5656 	IOPMSystemSleepParameters   params;
5657 	uint32_t                    hibMode = 0;
5658 	bool                        ok;
5659 
5660 	if (gIOPMWorkLoop->inGate() == false) {
5661 		IOReturn ret = gIOPMWorkLoop->runAction(
5662 			OSMemberFunctionCast(IOWorkLoop::Action, this,
5663 			&IOPMrootDomain::getSystemSleepType),
5664 			(OSObject *) this,
5665 			(void *) sleepType, (void *) standbyTimer);
5666 		return ret;
5667 	}
5668 
5669 	getSleepOption(kIOHibernateModeKey, &hibMode);
5670 	bzero(&params, sizeof(params));
5671 
5672 	ok = evaluateSystemSleepPolicy(&params, kIOPMSleepPhase0, &hibMode);
5673 	if (ok) {
5674 		*sleepType = params.sleepType;
5675 		if (!getSleepOption(kIOPMDeepSleepTimerKey, standbyTimer) &&
5676 		    !getSleepOption(kIOPMDeepSleepDelayKey, standbyTimer)) {
5677 			DLOG("Standby delay is not set\n");
5678 			*standbyTimer = 0;
5679 		}
5680 		return kIOReturnSuccess;
5681 	}
5682 #endif
5683 
5684 	return kIOReturnUnsupported;
5685 }
5686 
5687 // MARK: -
5688 // MARK: Shutdown and Restart
5689 
5690 //******************************************************************************
5691 // handlePlatformHaltRestart
5692 //
5693 //******************************************************************************
5694 
5695 // Phases while performing shutdown/restart
5696 typedef enum {
5697 	kNotifyDone                 = 0x00,
5698 	kNotifyPriorityClients      = 0x10,
5699 	kNotifyPowerPlaneDrivers    = 0x20,
5700 	kNotifyHaltRestartAction    = 0x30,
5701 	kQuiescePM                  = 0x40,
5702 } shutdownPhase_t;
5703 
5704 
5705 struct HaltRestartApplierContext {
5706 	IOPMrootDomain *    RootDomain;
5707 	unsigned long       PowerState;
5708 	IOPMPowerFlags      PowerFlags;
5709 	UInt32              MessageType;
5710 	UInt32              Counter;
5711 	const char *        LogString;
5712 	shutdownPhase_t     phase;
5713 
5714 	IOServiceInterestHandler    handler;
5715 } gHaltRestartCtx;
5716 
5717 const char *
5718 shutdownPhase2String(shutdownPhase_t phase)
5719 {
5720 	switch (phase) {
5721 	case kNotifyDone:
5722 		return "Notifications completed";
5723 	case kNotifyPriorityClients:
5724 		return "Notifying priority clients";
5725 	case kNotifyPowerPlaneDrivers:
5726 		return "Notifying power plane drivers";
5727 	case kNotifyHaltRestartAction:
5728 		return "Notifying HaltRestart action handlers";
5729 	case kQuiescePM:
5730 		return "Quiescing PM";
5731 	default:
5732 		return "Unknown";
5733 	}
5734 }
5735 
5736 static void
5737 platformHaltRestartApplier( OSObject * object, void * context )
5738 {
5739 	IOPowerStateChangeNotification  notify;
5740 	HaltRestartApplierContext *     ctx;
5741 	AbsoluteTime                    startTime, elapsedTime;
5742 	uint32_t                        deltaTime;
5743 
5744 	ctx = (HaltRestartApplierContext *) context;
5745 
5746 	_IOServiceInterestNotifier * notifier;
5747 	notifier = OSDynamicCast(_IOServiceInterestNotifier, object);
5748 	memset(&notify, 0, sizeof(notify));
5749 	notify.powerRef    = (void *)(uintptr_t)ctx->Counter;
5750 	notify.returnValue = 0;
5751 	notify.stateNumber = ctx->PowerState;
5752 	notify.stateFlags  = ctx->PowerFlags;
5753 
5754 	if (notifier) {
5755 		ctx->handler = notifier->handler;
5756 	}
5757 
5758 	clock_get_uptime(&startTime);
5759 	ctx->RootDomain->messageClient( ctx->MessageType, object, (void *)&notify );
5760 	deltaTime = computeDeltaTimeMS(&startTime, &elapsedTime);
5761 
5762 	if ((deltaTime > kPMHaltTimeoutMS) && notifier) {
5763 		LOG("%s handler %p took %u ms\n",
5764 		    ctx->LogString, OBFUSCATE(notifier->handler), deltaTime);
5765 		halt_log_enter("PowerOff/Restart message to priority client", (const void *) notifier->handler, elapsedTime);
5766 	}
5767 
5768 	ctx->handler = NULL;
5769 	ctx->Counter++;
5770 }
5771 
5772 static void
5773 quiescePowerTreeCallback( void * target, void * param )
5774 {
5775 	IOLockLock(gPMHaltLock);
5776 	gPMQuiesced = true;
5777 	thread_wakeup(param);
5778 	IOLockUnlock(gPMHaltLock);
5779 }
5780 
5781 void
5782 IOPMrootDomain::handlePlatformHaltRestart( UInt32 pe_type )
5783 {
5784 	AbsoluteTime                startTime, elapsedTime;
5785 	uint32_t                    deltaTime;
5786 	bool                        nvramSync = false;
5787 
5788 	memset(&gHaltRestartCtx, 0, sizeof(gHaltRestartCtx));
5789 	gHaltRestartCtx.RootDomain = this;
5790 
5791 	clock_get_uptime(&startTime);
5792 	switch (pe_type) {
5793 	case kPEHaltCPU:
5794 	case kPEUPSDelayHaltCPU:
5795 		gHaltRestartCtx.PowerState  = OFF_STATE;
5796 		gHaltRestartCtx.MessageType = kIOMessageSystemWillPowerOff;
5797 		gHaltRestartCtx.LogString   = "PowerOff";
5798 		nvramSync = true;
5799 		break;
5800 
5801 	case kPERestartCPU:
5802 		gHaltRestartCtx.PowerState  = RESTART_STATE;
5803 		gHaltRestartCtx.MessageType = kIOMessageSystemWillRestart;
5804 		gHaltRestartCtx.LogString   = "Restart";
5805 		nvramSync = true;
5806 		break;
5807 
5808 	case kPEPagingOff:
5809 		gHaltRestartCtx.PowerState  = ON_STATE;
5810 		gHaltRestartCtx.MessageType = kIOMessageSystemPagingOff;
5811 		gHaltRestartCtx.LogString   = "PagingOff";
5812 		IOService::updateConsoleUsers(NULL, kIOMessageSystemPagingOff);
5813 #if HIBERNATION
5814 		IOHibernateSystemRestart();
5815 #endif
5816 		break;
5817 
5818 	default:
5819 		return;
5820 	}
5821 
5822 	if (nvramSync) {
5823 		PESyncNVRAM();
5824 	}
5825 
5826 	gHaltRestartCtx.phase = kNotifyPriorityClients;
5827 	// Notify legacy clients
5828 	applyToInterested(gIOPriorityPowerStateInterest, platformHaltRestartApplier, &gHaltRestartCtx);
5829 
5830 	// For normal shutdown, turn off File Server Mode.
5831 	if (kPEHaltCPU == pe_type) {
5832 		OSSharedPtr<const OSSymbol> setting = OSSymbol::withCString(kIOPMSettingRestartOnPowerLossKey);
5833 		OSSharedPtr<OSNumber> num = OSNumber::withNumber((unsigned long long) 0, 32);
5834 		if (setting && num) {
5835 			setPMSetting(setting.get(), num.get());
5836 		}
5837 	}
5838 
5839 	if (kPEPagingOff != pe_type) {
5840 		gHaltRestartCtx.phase = kNotifyPowerPlaneDrivers;
5841 		// Notify in power tree order
5842 		notifySystemShutdown(this, gHaltRestartCtx.MessageType);
5843 	}
5844 
5845 	gHaltRestartCtx.phase = kNotifyHaltRestartAction;
5846 #if defined(XNU_TARGET_OS_OSX)
5847 	IOCPURunPlatformHaltRestartActions(pe_type);
5848 #else /* !defined(XNU_TARGET_OS_OSX) */
5849 	if (kPEPagingOff != pe_type) {
5850 		IOCPURunPlatformHaltRestartActions(pe_type);
5851 	}
5852 #endif /* !defined(XNU_TARGET_OS_OSX) */
5853 
5854 	// Wait for PM to quiesce
5855 	if ((kPEPagingOff != pe_type) && gPMHaltLock) {
5856 		gHaltRestartCtx.phase = kQuiescePM;
5857 		AbsoluteTime quiesceTime = mach_absolute_time();
5858 
5859 		IOLockLock(gPMHaltLock);
5860 		gPMQuiesced = false;
5861 		if (quiescePowerTree(this, &quiescePowerTreeCallback, &gPMQuiesced) ==
5862 		    kIOReturnSuccess) {
5863 			while (!gPMQuiesced) {
5864 				IOLockSleep(gPMHaltLock, &gPMQuiesced, THREAD_UNINT);
5865 			}
5866 		}
5867 		IOLockUnlock(gPMHaltLock);
5868 		deltaTime = computeDeltaTimeMS(&quiesceTime, &elapsedTime);
5869 		DLOG("PM quiesce took %u ms\n", deltaTime);
5870 		halt_log_enter("Quiesce", NULL, elapsedTime);
5871 	}
5872 	gHaltRestartCtx.phase = kNotifyDone;
5873 
5874 	deltaTime = computeDeltaTimeMS(&startTime, &elapsedTime);
5875 	LOG("%s all drivers took %u ms\n", gHaltRestartCtx.LogString, deltaTime);
5876 
5877 	halt_log_enter(gHaltRestartCtx.LogString, NULL, elapsedTime);
5878 
5879 	deltaTime = computeDeltaTimeMS(&gHaltStartTime, &elapsedTime);
5880 	LOG("%s total %u ms\n", gHaltRestartCtx.LogString, deltaTime);
5881 
5882 	if (gHaltLog && gHaltTimeMaxLog && (deltaTime >= gHaltTimeMaxLog)) {
5883 		printf("%s total %d ms:%s\n", gHaltRestartCtx.LogString, deltaTime, gHaltLog);
5884 	}
5885 
5886 	checkShutdownTimeout();
5887 }
5888 
5889 bool
5890 IOPMrootDomain::checkShutdownTimeout()
5891 {
5892 	AbsoluteTime   elapsedTime;
5893 	uint32_t deltaTime = computeDeltaTimeMS(&gHaltStartTime, &elapsedTime);
5894 
5895 	if (gHaltTimeMaxPanic && (deltaTime >= gHaltTimeMaxPanic)) {
5896 		return true;
5897 	}
5898 	return false;
5899 }
5900 
5901 void
5902 IOPMrootDomain::panicWithShutdownLog(uint32_t timeoutInMs)
5903 {
5904 	if (gHaltLog) {
5905 		if ((gHaltRestartCtx.phase == kNotifyPriorityClients) && gHaltRestartCtx.handler) {
5906 			halt_log_enter("Blocked on priority client", (void *)gHaltRestartCtx.handler, mach_absolute_time() - gHaltStartTime);
5907 		}
5908 		panic("%s timed out in phase '%s'. Total %d ms:%s",
5909 		    gHaltRestartCtx.LogString, shutdownPhase2String(gHaltRestartCtx.phase), timeoutInMs, gHaltLog);
5910 	} else {
5911 		panic("%s timed out in phase \'%s\'. Total %d ms",
5912 		    gHaltRestartCtx.LogString, shutdownPhase2String(gHaltRestartCtx.phase), timeoutInMs);
5913 	}
5914 }
5915 
5916 //******************************************************************************
5917 // shutdownSystem
5918 //
5919 //******************************************************************************
5920 
5921 IOReturn
5922 IOPMrootDomain::shutdownSystem( void )
5923 {
5924 	return kIOReturnUnsupported;
5925 }
5926 
5927 //******************************************************************************
5928 // restartSystem
5929 //
5930 //******************************************************************************
5931 
5932 IOReturn
5933 IOPMrootDomain::restartSystem( void )
5934 {
5935 	return kIOReturnUnsupported;
5936 }
5937 
5938 // MARK: -
5939 // MARK: System Capability
5940 
5941 //******************************************************************************
5942 // tagPowerPlaneService
5943 //
5944 // Running on PM work loop thread.
5945 //******************************************************************************
5946 
5947 void
5948 IOPMrootDomain::tagPowerPlaneService(
5949 	IOService *         service,
5950 	IOPMActions *       actions,
5951 	IOPMPowerStateIndex maxPowerState )
5952 {
5953 	uint32_t    flags = 0;
5954 
5955 	memset(actions, 0, sizeof(*actions));
5956 	actions->target = this;
5957 
5958 	if (service == this) {
5959 		actions->actionPowerChangeStart =
5960 		    OSMemberFunctionCast(
5961 			IOPMActionPowerChangeStart, this,
5962 			&IOPMrootDomain::handleOurPowerChangeStart);
5963 
5964 		actions->actionPowerChangeDone =
5965 		    OSMemberFunctionCast(
5966 			IOPMActionPowerChangeDone, this,
5967 			&IOPMrootDomain::handleOurPowerChangeDone);
5968 
5969 		actions->actionPowerChangeOverride =
5970 		    OSMemberFunctionCast(
5971 			IOPMActionPowerChangeOverride, this,
5972 			&IOPMrootDomain::overrideOurPowerChange);
5973 		return;
5974 	}
5975 
5976 #if DISPLAY_WRANGLER_PRESENT
5977 	if (NULL != service->metaCast("IODisplayWrangler")) {
5978 		// XXX should this really retain?
5979 		wrangler.reset(service, OSRetain);
5980 		wrangler->registerInterest(gIOGeneralInterest,
5981 		    &displayWranglerNotification, this, NULL);
5982 
5983 		// found the display wrangler, check for any display assertions already created
5984 		if (pmAssertions->getActivatedAssertions() & kIOPMDriverAssertionPreventDisplaySleepBit) {
5985 			DLOG("wrangler setIgnoreIdleTimer\(1) due to pre-existing assertion\n");
5986 			wrangler->setIgnoreIdleTimer( true );
5987 		}
5988 		flags |= kPMActionsFlagIsDisplayWrangler;
5989 	}
5990 #endif /* DISPLAY_WRANGLER_PRESENT */
5991 
5992 	if (service->propertyExists("IOPMStrictTreeOrder")) {
5993 		flags |= kPMActionsFlagIsGraphicsDriver;
5994 	}
5995 	if (service->propertyExists("IOPMUnattendedWakePowerState")) {
5996 		flags |= kPMActionsFlagIsAudioDriver;
5997 	}
5998 
5999 	// Find the power connection object that is a child of the PCI host
6000 	// bridge, and has a graphics/audio device attached below. Mark the
6001 	// power branch for delayed child notifications.
6002 
6003 	if (flags) {
6004 		IORegistryEntry * child  = service;
6005 		IORegistryEntry * parent = child->getParentEntry(gIOPowerPlane);
6006 
6007 		while (child != this) {
6008 			if (child->propertyHasValue("IOPCITunnelled", kOSBooleanTrue)) {
6009 				// Skip delaying notifications and clamping power on external graphics and audio devices.
6010 				DLOG("Avoiding delayChildNotification on object 0x%llx. flags: 0x%x\n", service->getRegistryEntryID(), flags);
6011 				flags = 0;
6012 				break;
6013 			}
6014 			if ((parent == pciHostBridgeDriver) ||
6015 			    (parent == this)) {
6016 				if (OSDynamicCast(IOPowerConnection, child)) {
6017 					IOPowerConnection * conn = (IOPowerConnection *) child;
6018 					conn->delayChildNotification = true;
6019 					DLOG("delayChildNotification for 0x%llx\n", conn->getRegistryEntryID());
6020 				}
6021 				break;
6022 			}
6023 			child = parent;
6024 			parent = child->getParentEntry(gIOPowerPlane);
6025 		}
6026 	}
6027 
6028 	OSSharedPtr<OSObject> prop = service->copyProperty(kIOPMDarkWakeMaxPowerStateKey);
6029 	if (prop) {
6030 		OSNumber * num = OSDynamicCast(OSNumber, prop.get());
6031 		if (num) {
6032 			actions->darkWakePowerState = num->unsigned32BitValue();
6033 			if (actions->darkWakePowerState < maxPowerState) {
6034 				flags |= kPMActionsFlagHasDarkWakePowerState;
6035 			}
6036 		}
6037 	}
6038 
6039 
6040 	if (flags) {
6041 		DLOG("%s tag flags %x\n", service->getName(), flags);
6042 		actions->flags |= flags;
6043 		actions->actionPowerChangeOverride =
6044 		    OSMemberFunctionCast(
6045 			IOPMActionPowerChangeOverride, this,
6046 			&IOPMrootDomain::overridePowerChangeForService);
6047 
6048 		if (flags & kPMActionsFlagIsDisplayWrangler) {
6049 			actions->actionActivityTickle =
6050 			    OSMemberFunctionCast(
6051 				IOPMActionActivityTickle, this,
6052 				&IOPMrootDomain::handleActivityTickleForDisplayWrangler);
6053 
6054 			actions->actionUpdatePowerClient =
6055 			    OSMemberFunctionCast(
6056 				IOPMActionUpdatePowerClient, this,
6057 				&IOPMrootDomain::handleUpdatePowerClientForDisplayWrangler);
6058 		}
6059 		return;
6060 	}
6061 
6062 	// Locate the first PCI host bridge for PMTrace.
6063 	if (!pciHostBridgeDevice && service->metaCast("IOPCIBridge")) {
6064 		IOService * provider = service->getProvider();
6065 		if (OSDynamicCast(IOPlatformDevice, provider) &&
6066 		    provider->inPlane(gIODTPlane)) {
6067 			pciHostBridgeDevice.reset(provider, OSNoRetain);
6068 			pciHostBridgeDriver.reset(service, OSNoRetain);
6069 			DLOG("PMTrace found PCI host bridge %s->%s\n",
6070 			    provider->getName(), service->getName());
6071 		}
6072 	}
6073 
6074 	// Tag top-level PCI devices. The order of PMinit() call does not
6075 	// change across boots and is used as the PCI bit number.
6076 	if (pciHostBridgeDevice && service->metaCast("IOPCIDevice")) {
6077 		// Would prefer to check built-in property, but tagPowerPlaneService()
6078 		// is called before pciDevice->registerService().
6079 		IORegistryEntry * parent = service->getParentEntry(gIODTPlane);
6080 		if ((parent == pciHostBridgeDevice) && service->propertyExists("acpi-device")) {
6081 			int bit = pmTracer->recordTopLevelPCIDevice( service );
6082 			if (bit >= 0) {
6083 				// Save the assigned bit for fast lookup.
6084 				actions->flags |= (bit & kPMActionsPCIBitNumberMask);
6085 
6086 				actions->actionPowerChangeStart =
6087 				    OSMemberFunctionCast(
6088 					IOPMActionPowerChangeStart, this,
6089 					&IOPMrootDomain::handlePowerChangeStartForPCIDevice);
6090 
6091 				actions->actionPowerChangeDone =
6092 				    OSMemberFunctionCast(
6093 					IOPMActionPowerChangeDone, this,
6094 					&IOPMrootDomain::handlePowerChangeDoneForPCIDevice);
6095 			}
6096 		}
6097 	}
6098 }
6099 
6100 //******************************************************************************
6101 // PM actions for root domain
6102 //******************************************************************************
6103 
6104 void
6105 IOPMrootDomain::overrideOurPowerChange(
6106 	IOService *             service,
6107 	IOPMActions *           actions,
6108 	const IOPMRequest *     request,
6109 	IOPMPowerStateIndex *   inOutPowerState,
6110 	IOPMPowerChangeFlags *  inOutChangeFlags )
6111 {
6112 	uint32_t changeFlags = *inOutChangeFlags;
6113 	uint32_t desiredPowerState = (uint32_t) *inOutPowerState;
6114 	uint32_t currentPowerState = (uint32_t) getPowerState();
6115 
6116 	if (request->getTag() == 0) {
6117 		// Set a tag for any request that originates from IOServicePM
6118 		(const_cast<IOPMRequest *>(request))->fTag = nextRequestTag(kCPSReasonPMInternals);
6119 	}
6120 
6121 	DLOG("PowerChangeOverride (%s->%s, %x, 0x%x) tag 0x%x\n",
6122 	    getPowerStateString(currentPowerState),
6123 	    getPowerStateString(desiredPowerState),
6124 	    _currentCapability, changeFlags,
6125 	    request->getTag());
6126 
6127 
6128 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
6129 	/*
6130 	 * ASBM send lowBattery notifications every 1 second until the device
6131 	 * enters hibernation. This queues up multiple sleep requests.
6132 	 * After the device wakes from hibernation, none of these previously
6133 	 * queued sleep requests are valid.
6134 	 * lowBattteryCondition variable is set when ASBM notifies rootDomain
6135 	 * and is cleared at the very last point in sleep.
6136 	 * Any attempt to sleep with reason kIOPMSleepReasonLowPower without
6137 	 * lowBatteryCondition is invalid
6138 	 */
6139 	if (REQUEST_TAG_TO_REASON(request->getTag()) == kIOPMSleepReasonLowPower) {
6140 		if (!lowBatteryCondition) {
6141 			DLOG("Duplicate lowBattery sleep");
6142 			*inOutChangeFlags |= kIOPMNotDone;
6143 			return;
6144 		}
6145 	}
6146 #endif
6147 
6148 	if ((AOT_STATE == desiredPowerState) && (ON_STATE == currentPowerState)) {
6149 		// Assertion may have been taken in AOT leading to changePowerStateTo(AOT)
6150 		*inOutChangeFlags |= kIOPMNotDone;
6151 		return;
6152 	}
6153 
6154 	if (changeFlags & kIOPMParentInitiated) {
6155 		// Root parent is permanently pegged at max power,
6156 		// a parent initiated power change is unexpected.
6157 		*inOutChangeFlags |= kIOPMNotDone;
6158 		return;
6159 	}
6160 
6161 #if HIBERNATION && defined(__arm64__)
6162 	if (lowBatteryCondition && (desiredPowerState < currentPowerState)) {
6163 		if (!ml_is_secure_hib_supported()) {
6164 			// If hibernation is unsupported, reject sleep requests to avoid
6165 			// racing with system shutdown.
6166 			*inOutChangeFlags |= kIOPMNotDone;
6167 			return;
6168 		}
6169 	}
6170 #endif /* HIBERNATION && defined(__arm64__) */
6171 
6172 	if (desiredPowerState < currentPowerState) {
6173 		if (CAP_CURRENT(kIOPMSystemCapabilityGraphics)) {
6174 			// Root domain is dropping power state from ON->SLEEP.
6175 			// If system is in full wake, first enter dark wake by
6176 			// converting the power drop to a capability change.
6177 			// Once in dark wake, transition to sleep state ASAP.
6178 
6179 			darkWakeToSleepASAP = true;
6180 
6181 			// Drop graphics and audio capability
6182 			_desiredCapability &= ~(
6183 				kIOPMSystemCapabilityGraphics |
6184 				kIOPMSystemCapabilityAudio);
6185 
6186 			// Convert to capability change (ON->ON)
6187 			*inOutPowerState = getRUN_STATE();
6188 			*inOutChangeFlags |= kIOPMSynchronize;
6189 
6190 			// Revert device desire from SLEEP to ON
6191 			changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonPowerOverride);
6192 		} else {
6193 			// System is already in dark wake, ok to drop power state.
6194 			// Broadcast root power down to entire tree.
6195 			*inOutChangeFlags |= kIOPMRootChangeDown;
6196 		}
6197 	} else if (desiredPowerState > currentPowerState) {
6198 		if ((_currentCapability & kIOPMSystemCapabilityCPU) == 0) {
6199 			// Broadcast power up when waking from sleep, but not for the
6200 			// initial power change at boot by checking for cpu capability.
6201 			*inOutChangeFlags |= kIOPMRootChangeUp;
6202 		}
6203 	}
6204 }
6205 
6206 void
6207 IOPMrootDomain::handleOurPowerChangeStart(
6208 	IOService *             service,
6209 	IOPMActions *           actions,
6210 	const IOPMRequest *     request,
6211 	IOPMPowerStateIndex     newPowerState,
6212 	IOPMPowerChangeFlags *  inOutChangeFlags )
6213 {
6214 	IOPMRequestTag requestTag = request->getTag();
6215 	IOPMRequestTag sleepReason;
6216 
6217 	uint32_t changeFlags        = *inOutChangeFlags;
6218 	uint32_t currentPowerState  = (uint32_t) getPowerState();
6219 	bool     publishSleepReason = false;
6220 
6221 	// Check if request has a valid sleep reason
6222 	sleepReason = REQUEST_TAG_TO_REASON(requestTag);
6223 	if (sleepReason < kIOPMSleepReasonClamshell) {
6224 		sleepReason = kIOPMSleepReasonIdle;
6225 	}
6226 
6227 	_systemTransitionType    = kSystemTransitionNone;
6228 	_systemMessageClientMask = 0;
6229 	capabilityLoss           = false;
6230 	toldPowerdCapWillChange  = false;
6231 
6232 	// Emergency notifications may arrive after the initial sleep request
6233 	// has been queued. Override the sleep reason so powerd and others can
6234 	// treat this as an emergency sleep.
6235 	if (lowBatteryCondition) {
6236 		sleepReason = kIOPMSleepReasonLowPower;
6237 	} else if (thermalEmergencyState) {
6238 		sleepReason = kIOPMSleepReasonThermalEmergency;
6239 	}
6240 
6241 	// 1. Explicit capability change.
6242 	if (changeFlags & kIOPMSynchronize) {
6243 		if (newPowerState == ON_STATE) {
6244 			if (changeFlags & kIOPMSyncNoChildNotify) {
6245 				_systemTransitionType = kSystemTransitionNewCapClient;
6246 			} else {
6247 				_systemTransitionType = kSystemTransitionCapability;
6248 			}
6249 		}
6250 	}
6251 	// 2. Going to sleep (cancellation still possible).
6252 	else if (newPowerState < currentPowerState) {
6253 		_systemTransitionType = kSystemTransitionSleep;
6254 	}
6255 	// 3. Woke from (idle or demand) sleep.
6256 	else if (!systemBooting &&
6257 	    (changeFlags & kIOPMSelfInitiated) &&
6258 	    (newPowerState > currentPowerState)) {
6259 		_systemTransitionType = kSystemTransitionWake;
6260 		_desiredCapability = kIOPMSystemCapabilityCPU | kIOPMSystemCapabilityNetwork;
6261 
6262 		// Early exit from dark wake to full (e.g. LID open)
6263 		if (kFullWakeReasonNone != fullWakeReason) {
6264 			_desiredCapability |= (
6265 				kIOPMSystemCapabilityGraphics |
6266 				kIOPMSystemCapabilityAudio);
6267 
6268 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
6269 			if (fullWakeReason == kFullWakeReasonLocalUser) {
6270 				darkWakeExit = true;
6271 				darkWakeToSleepASAP = false;
6272 				setProperty(kIOPMRootDomainWakeTypeKey, isRTCAlarmWake ?
6273 				    kIOPMRootDomainWakeTypeAlarm : kIOPMRootDomainWakeTypeUser);
6274 			}
6275 #endif
6276 		}
6277 #if HIBERNATION
6278 		IOHibernateSetWakeCapabilities(_desiredCapability);
6279 #endif
6280 	}
6281 
6282 	// Update pending wake capability at the beginning of every
6283 	// state transition (including synchronize). This will become
6284 	// the current capability at the end of the transition.
6285 
6286 	if (kSystemTransitionSleep == _systemTransitionType) {
6287 		_pendingCapability = 0;
6288 		capabilityLoss = true;
6289 	} else if (kSystemTransitionNewCapClient != _systemTransitionType) {
6290 		_pendingCapability = _desiredCapability |
6291 		    kIOPMSystemCapabilityCPU |
6292 		    kIOPMSystemCapabilityNetwork;
6293 
6294 		if (_pendingCapability & kIOPMSystemCapabilityGraphics) {
6295 			_pendingCapability |= kIOPMSystemCapabilityAudio;
6296 		}
6297 
6298 		if ((kSystemTransitionCapability == _systemTransitionType) &&
6299 		    (_pendingCapability == _currentCapability)) {
6300 			// Cancel the PM state change.
6301 			_systemTransitionType = kSystemTransitionNone;
6302 			*inOutChangeFlags |= kIOPMNotDone;
6303 		}
6304 		if (__builtin_popcount(_pendingCapability) <
6305 		    __builtin_popcount(_currentCapability)) {
6306 			capabilityLoss = true;
6307 		}
6308 	}
6309 
6310 	// 1. Capability change.
6311 	if (kSystemTransitionCapability == _systemTransitionType) {
6312 		// Dark to Full transition.
6313 		if (CAP_GAIN(kIOPMSystemCapabilityGraphics)) {
6314 			tracePoint( kIOPMTracePointDarkWakeExit );
6315 
6316 #if defined(XNU_TARGET_OS_OSX)
6317 			// rdar://problem/65627936
6318 			// When a dark->full wake promotion is scheduled before an ON->SLEEP
6319 			// power state drop, invalidate any request to drop power state already
6320 			// in the queue, including the override variant, unless full wake cannot
6321 			// be sustained. Any power state drop queued after this SustainFullWake
6322 			// request will not be affected.
6323 			if (checkSystemCanSustainFullWake()) {
6324 				changePowerStateWithOverrideTo(getRUN_STATE(), kCPSReasonSustainFullWake);
6325 			}
6326 #endif
6327 
6328 			willEnterFullWake();
6329 		}
6330 
6331 		// Full to Dark transition.
6332 		if (CAP_LOSS(kIOPMSystemCapabilityGraphics)) {
6333 			// Clear previous stats
6334 			IOLockLock(pmStatsLock);
6335 			if (pmStatsAppResponses) {
6336 				pmStatsAppResponses = OSArray::withCapacity(5);
6337 			}
6338 			IOLockUnlock(pmStatsLock);
6339 
6340 			tracePoint( kIOPMTracePointDarkWakeEntry );
6341 			*inOutChangeFlags |= kIOPMSyncTellPowerDown;
6342 			_systemMessageClientMask = kSystemMessageClientPowerd |
6343 			    kSystemMessageClientLegacyApp;
6344 
6345 			// rdar://15971327
6346 			// Prevent user active transitions before notifying clients
6347 			// that system will sleep.
6348 			preventTransitionToUserActive(true);
6349 
6350 			IOService::setAdvisoryTickleEnable( false );
6351 
6352 			// Publish the sleep reason for full to dark wake
6353 			publishSleepReason = true;
6354 			lastSleepReason = fullToDarkReason = sleepReason;
6355 
6356 			// Publish a UUID for the Sleep --> Wake cycle
6357 			handlePublishSleepWakeUUID(true);
6358 			if (sleepDelaysReport) {
6359 				clock_get_uptime(&ts_sleepStart);
6360 				DLOG("sleepDelaysReport f->9 start at 0x%llx\n", ts_sleepStart);
6361 			}
6362 
6363 			darkWakeExit = false;
6364 		}
6365 	}
6366 	// 2. System sleep.
6367 	else if (kSystemTransitionSleep == _systemTransitionType) {
6368 		// Beginning of a system sleep transition.
6369 		// Cancellation is still possible.
6370 		tracePoint( kIOPMTracePointSleepStarted );
6371 
6372 		_systemMessageClientMask = kSystemMessageClientAll;
6373 		if ((_currentCapability & kIOPMSystemCapabilityGraphics) == 0) {
6374 			_systemMessageClientMask &= ~kSystemMessageClientLegacyApp;
6375 		}
6376 		if ((_highestCapability & kIOPMSystemCapabilityGraphics) == 0) {
6377 			// Kernel priority clients are only notified on the initial
6378 			// transition to full wake, so don't notify them unless system
6379 			// has gained graphics capability since the last system wake.
6380 			_systemMessageClientMask &= ~kSystemMessageClientKernel;
6381 		} else {
6382 			// System was in full wake, but the downwards power transition is driven
6383 			// by a request that originates from IOServicePM, so it isn't tagged with
6384 			// a valid system sleep reason.
6385 			if (REQUEST_TAG_TO_REASON(requestTag) == kCPSReasonPMInternals) {
6386 				// Publish the same reason for full to dark
6387 				sleepReason = fullToDarkReason;
6388 			}
6389 		}
6390 #if HIBERNATION
6391 		gIOHibernateState = 0;
6392 #endif
6393 
6394 		// Record the reason for dark wake back to sleep
6395 		// System may not have ever achieved full wake
6396 
6397 		publishSleepReason = true;
6398 		lastSleepReason = sleepReason;
6399 		if (sleepDelaysReport) {
6400 			clock_get_uptime(&ts_sleepStart);
6401 			DLOG("sleepDelaysReport 9->0 start at 0x%llx\n", ts_sleepStart);
6402 		}
6403 	}
6404 	// 3. System wake.
6405 	else if (kSystemTransitionWake == _systemTransitionType) {
6406 		tracePoint( kIOPMTracePointWakeWillPowerOnClients );
6407 		// Clear stats about sleep
6408 
6409 		if (AOT_STATE == newPowerState) {
6410 			_pendingCapability = 0;
6411 		}
6412 
6413 		if (AOT_STATE == currentPowerState) {
6414 			// Wake events are no longer accepted after waking to AOT_STATE.
6415 			// Re-enable wake event acceptance to append wake events claimed
6416 			// during the AOT to ON_STATE transition.
6417 			acceptSystemWakeEvents(kAcceptSystemWakeEvents_Reenable);
6418 		}
6419 
6420 		if (_pendingCapability & kIOPMSystemCapabilityGraphics) {
6421 			willEnterFullWake();
6422 		}
6423 	}
6424 
6425 	// The only location where the sleep reason is published. At this point
6426 	// sleep can still be cancelled, but sleep reason should be published
6427 	// early for logging purposes.
6428 
6429 	if (publishSleepReason) {
6430 		static const char * IOPMSleepReasons[] =
6431 		{
6432 			kIOPMClamshellSleepKey,
6433 			kIOPMPowerButtonSleepKey,
6434 			kIOPMSoftwareSleepKey,
6435 			kIOPMOSSwitchHibernationKey,
6436 			kIOPMIdleSleepKey,
6437 			kIOPMLowPowerSleepKey,
6438 			kIOPMThermalEmergencySleepKey,
6439 			kIOPMMaintenanceSleepKey,
6440 			kIOPMSleepServiceExitKey,
6441 			kIOPMDarkWakeThermalEmergencyKey,
6442 			kIOPMNotificationWakeExitKey
6443 		};
6444 
6445 		// Record sleep cause in IORegistry
6446 		uint32_t reasonIndex = sleepReason - kIOPMSleepReasonClamshell;
6447 		if (reasonIndex < sizeof(IOPMSleepReasons) / sizeof(IOPMSleepReasons[0])) {
6448 			DLOG("sleep reason %s\n", IOPMSleepReasons[reasonIndex]);
6449 #if DEVELOPMENT || DEBUG
6450 			record_system_event(SYSTEM_EVENT_TYPE_INFO,
6451 			    SYSTEM_EVENT_SUBSYSTEM_PMRD,
6452 			    "Sleep Reason", "%s\n", IOPMSleepReasons[reasonIndex]
6453 			    );
6454 #endif /* DEVELOPMENT || DEBUG */
6455 			setProperty(kRootDomainSleepReasonKey, IOPMSleepReasons[reasonIndex]);
6456 		}
6457 	}
6458 
6459 	if ((kSystemTransitionNone != _systemTransitionType) &&
6460 	    (kSystemTransitionNewCapClient != _systemTransitionType)) {
6461 		_systemStateGeneration++;
6462 		systemDarkWake = false;
6463 
6464 		DLOG("=== START (%s->%s, %x->%x, 0x%x) gen %u, msg %x, tag %x\n",
6465 		    getPowerStateString(currentPowerState),
6466 		    getPowerStateString((uint32_t) newPowerState),
6467 		    _currentCapability, _pendingCapability,
6468 		    *inOutChangeFlags, _systemStateGeneration, _systemMessageClientMask,
6469 		    requestTag);
6470 #if DEVELOPMENT || DEBUG
6471 		if (currentPowerState != (uint32_t) newPowerState) {
6472 			record_system_event(SYSTEM_EVENT_TYPE_INFO,
6473 			    SYSTEM_EVENT_SUBSYSTEM_PMRD,
6474 			    "Start Power State Trans.",
6475 			    "(%s->%s, %x->%x, 0x%x) gen %u, msg %x, tag %x\n",
6476 			    getPowerStateString(currentPowerState),
6477 			    getPowerStateString((uint32_t) newPowerState),
6478 			    _currentCapability,
6479 			    _pendingCapability,
6480 			    *inOutChangeFlags,
6481 			    _systemStateGeneration,
6482 			    _systemMessageClientMask,
6483 			    requestTag
6484 			    );
6485 		}
6486 #endif /* DEVELOPMENT || DEBUG */
6487 	}
6488 
6489 	if ((AOT_STATE == newPowerState) && (SLEEP_STATE != currentPowerState)) {
6490 		panic("illegal AOT entry from %s", getPowerStateString(currentPowerState));
6491 	}
6492 	if (_aotNow && (ON_STATE == newPowerState)) {
6493 		WAKEEVENT_LOCK();
6494 		aotShouldExit(true);
6495 		WAKEEVENT_UNLOCK();
6496 		aotExit(false);
6497 	}
6498 }
6499 
6500 void
6501 IOPMrootDomain::handleOurPowerChangeDone(
6502 	IOService *             service,
6503 	IOPMActions *           actions,
6504 	const IOPMRequest *     request,
6505 	IOPMPowerStateIndex     oldPowerState,
6506 	IOPMPowerChangeFlags    changeFlags )
6507 {
6508 	if (kSystemTransitionNewCapClient == _systemTransitionType) {
6509 		_systemTransitionType = kSystemTransitionNone;
6510 		return;
6511 	}
6512 
6513 	if (_systemTransitionType != kSystemTransitionNone) {
6514 		uint32_t currentPowerState = (uint32_t) getPowerState();
6515 
6516 		if (changeFlags & kIOPMNotDone) {
6517 			// Power down was cancelled or vetoed.
6518 			_pendingCapability = _currentCapability;
6519 			lastSleepReason = 0;
6520 
6521 			// When sleep is cancelled or reverted, don't report
6522 			// the target (lower) power state as the previous state.
6523 			oldPowerState = currentPowerState;
6524 
6525 			if (!CAP_CURRENT(kIOPMSystemCapabilityGraphics) &&
6526 			    CAP_CURRENT(kIOPMSystemCapabilityCPU)) {
6527 #if defined(XNU_TARGET_OS_OSX)
6528 				pmPowerStateQueue->submitPowerEvent(
6529 					kPowerEventPolicyStimulus,
6530 					(void *) kStimulusDarkWakeReentry,
6531 					_systemStateGeneration );
6532 #else /* !defined(XNU_TARGET_OS_OSX) */
6533 				// On embedded, there are no factors that can prolong a
6534 				// "darkWake" when a power down is vetoed. We need to
6535 				// promote to "fullWake" at least once so that factors
6536 				// that prevent idle sleep can assert themselves if required
6537 				pmPowerStateQueue->submitPowerEvent(
6538 					kPowerEventPolicyStimulus,
6539 					(void *) kStimulusDarkWakeActivityTickle);
6540 #endif /* !defined(XNU_TARGET_OS_OSX) */
6541 			}
6542 
6543 			// Revert device desire to max.
6544 			changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonPowerDownCancel);
6545 		} else {
6546 			// Send message on dark wake to full wake promotion.
6547 			// tellChangeUp() handles the normal SLEEP->ON case.
6548 
6549 			if (kSystemTransitionCapability == _systemTransitionType) {
6550 				if (CAP_GAIN(kIOPMSystemCapabilityGraphics)) {
6551 					lastSleepReason = 0; // stop logging wrangler tickles
6552 					tellClients(kIOMessageSystemHasPoweredOn);
6553 				}
6554 				if (CAP_LOSS(kIOPMSystemCapabilityGraphics)) {
6555 					// Going dark, reset full wake state
6556 					// userIsActive will be cleared by wrangler powering down
6557 					fullWakeReason = kFullWakeReasonNone;
6558 
6559 					if (ts_sleepStart) {
6560 						clock_get_uptime(&wake2DarkwakeDelay);
6561 						SUB_ABSOLUTETIME(&wake2DarkwakeDelay, &ts_sleepStart);
6562 						DLOG("sleepDelaysReport f->9 end 0x%llx\n", wake2DarkwakeDelay);
6563 						ts_sleepStart = 0;
6564 					}
6565 				}
6566 			}
6567 
6568 			// Reset state after exiting from dark wake.
6569 
6570 			if (CAP_GAIN(kIOPMSystemCapabilityGraphics) ||
6571 			    CAP_LOSS(kIOPMSystemCapabilityCPU)) {
6572 				darkWakeMaintenance = false;
6573 				darkWakeToSleepASAP = false;
6574 				pciCantSleepValid   = false;
6575 				darkWakeSleepService = false;
6576 
6577 				if (CAP_LOSS(kIOPMSystemCapabilityCPU)) {
6578 					// Remove the influence of display power assertion
6579 					// before next system wake.
6580 					if (wrangler) {
6581 						wrangler->changePowerStateForRootDomain(
6582 							kWranglerPowerStateMin );
6583 					}
6584 					removeProperty(gIOPMUserTriggeredFullWakeKey.get());
6585 				}
6586 			}
6587 
6588 			// Entered dark mode.
6589 
6590 			if (((_pendingCapability & kIOPMSystemCapabilityGraphics) == 0) &&
6591 			    (_pendingCapability & kIOPMSystemCapabilityCPU)) {
6592 				// Queue an evaluation of whether to remain in dark wake,
6593 				// and for how long. This serves the purpose of draining
6594 				// any assertions from the queue.
6595 
6596 				pmPowerStateQueue->submitPowerEvent(
6597 					kPowerEventPolicyStimulus,
6598 					(void *) kStimulusDarkWakeEntry,
6599 					_systemStateGeneration );
6600 			}
6601 		}
6602 
6603 #if DEVELOPMENT || DEBUG
6604 		if (currentPowerState != (uint32_t) oldPowerState) {
6605 			record_system_event(SYSTEM_EVENT_TYPE_INFO,
6606 			    SYSTEM_EVENT_SUBSYSTEM_PMRD,
6607 			    "Finish Power State Trans.",
6608 			    "(%s->%s, %x->%x, 0x%x) gen %u, msg %x, tag %x\n",
6609 			    getPowerStateString((uint32_t)oldPowerState),
6610 			    getPowerStateString(currentPowerState),
6611 			    _currentCapability,
6612 			    _pendingCapability,
6613 			    changeFlags,
6614 			    _systemStateGeneration,
6615 			    _systemMessageClientMask,
6616 			    request->getTag()
6617 			    );
6618 		}
6619 #endif /* DEVELOPMENT || DEBUG */
6620 
6621 		DLOG("=== FINISH (%s->%s, %x->%x, 0x%x) gen %u, msg %x, tag %x\n",
6622 		    getPowerStateString((uint32_t) oldPowerState), getPowerStateString(currentPowerState),
6623 		    _currentCapability, _pendingCapability,
6624 		    changeFlags, _systemStateGeneration, _systemMessageClientMask,
6625 		    request->getTag());
6626 
6627 		if ((currentPowerState == ON_STATE) && pmAssertions) {
6628 			pmAssertions->reportCPUBitAccounting();
6629 		}
6630 
6631 		if (_pendingCapability & kIOPMSystemCapabilityGraphics) {
6632 			displayWakeCnt++;
6633 #if DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY
6634 			if (clamshellExists && fullWakeThreadCall) {
6635 				AbsoluteTime deadline;
6636 				clock_interval_to_deadline(DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY, kSecondScale, &deadline);
6637 				thread_call_enter_delayed(fullWakeThreadCall, deadline);
6638 			}
6639 #endif
6640 		} else if (CAP_GAIN(kIOPMSystemCapabilityCPU)) {
6641 			darkWakeCnt++;
6642 		}
6643 
6644 		// Update current system capability.
6645 		if (_currentCapability != _pendingCapability) {
6646 			_currentCapability = _pendingCapability;
6647 		}
6648 
6649 		// Update highest system capability.
6650 
6651 		_highestCapability |= _currentCapability;
6652 
6653 		if (darkWakePostTickle &&
6654 		    (kSystemTransitionWake == _systemTransitionType) &&
6655 		    (gDarkWakeFlags & kDarkWakeFlagPromotionMask) ==
6656 		    kDarkWakeFlagPromotionLate) {
6657 			darkWakePostTickle = false;
6658 			reportUserInput();
6659 		} else if (darkWakeExit) {
6660 			requestFullWake( kFullWakeReasonLocalUser );
6661 		}
6662 
6663 		// Reset tracepoint at completion of capability change,
6664 		// completion of wake transition, and aborted sleep transition.
6665 
6666 		if ((_systemTransitionType == kSystemTransitionCapability) ||
6667 		    (_systemTransitionType == kSystemTransitionWake) ||
6668 		    ((_systemTransitionType == kSystemTransitionSleep) &&
6669 		    (changeFlags & kIOPMNotDone))) {
6670 			setProperty(kIOPMSystemCapabilitiesKey, _currentCapability, 64);
6671 			tracePoint( kIOPMTracePointSystemUp );
6672 		}
6673 
6674 		_systemTransitionType = kSystemTransitionNone;
6675 		_systemMessageClientMask = 0;
6676 		toldPowerdCapWillChange  = false;
6677 
6678 		darkWakeLogClamp = false;
6679 
6680 		if (lowBatteryCondition) {
6681 			privateSleepSystem(kIOPMSleepReasonLowPower);
6682 		} else if (thermalEmergencyState) {
6683 			privateSleepSystem(kIOPMSleepReasonThermalEmergency);
6684 		} else if ((fullWakeReason == kFullWakeReasonDisplayOn) && !displayPowerOnRequested) {
6685 			// Request for full wake is removed while system is waking up to full wake
6686 			DLOG("DisplayOn fullwake request is removed\n");
6687 			handleSetDisplayPowerOn(false);
6688 		}
6689 
6690 		if ((gClamshellFlags & kClamshell_WAR_47715679) && isRTCAlarmWake) {
6691 			pmPowerStateQueue->submitPowerEvent(
6692 				kPowerEventReceivedPowerNotification, (void *)(uintptr_t) kLocalEvalClamshellCommand );
6693 		}
6694 	}
6695 }
6696 
6697 //******************************************************************************
6698 // PM actions for graphics and audio.
6699 //******************************************************************************
6700 
6701 void
6702 IOPMrootDomain::overridePowerChangeForService(
6703 	IOService *             service,
6704 	IOPMActions *           actions,
6705 	const IOPMRequest *     request,
6706 	IOPMPowerStateIndex *   inOutPowerState,
6707 	IOPMPowerChangeFlags *  inOutChangeFlags )
6708 {
6709 	uint32_t powerState  = (uint32_t) *inOutPowerState;
6710 	uint32_t changeFlags = (uint32_t) *inOutChangeFlags;
6711 	const uint32_t actionFlags = actions->flags;
6712 
6713 	if (kSystemTransitionNone == _systemTransitionType) {
6714 		// Not in midst of a system transition.
6715 		// Do not set kPMActionsStatePowerClamped.
6716 	} else if ((actions->state & kPMActionsStatePowerClamped) == 0) {
6717 		bool enableClamp = false;
6718 
6719 		// For most drivers, enable the clamp during ON->Dark transition
6720 		// which has the kIOPMSynchronize flag set in changeFlags.
6721 		if ((actionFlags & kPMActionsFlagIsDisplayWrangler) &&
6722 		    ((_pendingCapability & kIOPMSystemCapabilityGraphics) == 0) &&
6723 		    (changeFlags & kIOPMSynchronize)) {
6724 			enableClamp = true;
6725 		} else if ((actionFlags & kPMActionsFlagIsAudioDriver) &&
6726 		    ((gDarkWakeFlags & kDarkWakeFlagAudioNotSuppressed) == 0) &&
6727 		    ((_pendingCapability & kIOPMSystemCapabilityAudio) == 0) &&
6728 		    (changeFlags & kIOPMSynchronize)) {
6729 			enableClamp = true;
6730 		} else if ((actionFlags & kPMActionsFlagHasDarkWakePowerState) &&
6731 		    ((_pendingCapability & kIOPMSystemCapabilityGraphics) == 0) &&
6732 		    (changeFlags & kIOPMSynchronize)) {
6733 			enableClamp = true;
6734 		} else if ((actionFlags & kPMActionsFlagIsGraphicsDriver) &&
6735 		    (_systemTransitionType == kSystemTransitionSleep)) {
6736 			// For graphics drivers, clamp power when entering
6737 			// system sleep. Not when dropping to dark wake.
6738 			enableClamp = true;
6739 		}
6740 
6741 		if (enableClamp) {
6742 			actions->state |= kPMActionsStatePowerClamped;
6743 			DLOG("power clamp enabled %s %qx, pendingCap 0x%x, ps %d, cflags 0x%x\n",
6744 			    service->getName(), service->getRegistryEntryID(),
6745 			    _pendingCapability, powerState, changeFlags);
6746 		}
6747 	} else if ((actions->state & kPMActionsStatePowerClamped) != 0) {
6748 		bool disableClamp = false;
6749 
6750 		if ((actionFlags & (
6751 			    kPMActionsFlagIsDisplayWrangler |
6752 			    kPMActionsFlagIsGraphicsDriver)) &&
6753 		    (_pendingCapability & kIOPMSystemCapabilityGraphics)) {
6754 			disableClamp = true;
6755 		} else if ((actionFlags & kPMActionsFlagIsAudioDriver) &&
6756 		    (_pendingCapability & kIOPMSystemCapabilityAudio)) {
6757 			disableClamp = true;
6758 		} else if ((actionFlags & kPMActionsFlagHasDarkWakePowerState) &&
6759 		    (_pendingCapability & kIOPMSystemCapabilityGraphics)) {
6760 			disableClamp = true;
6761 		}
6762 
6763 		if (disableClamp) {
6764 			actions->state &= ~kPMActionsStatePowerClamped;
6765 			DLOG("power clamp removed %s %qx, pendingCap 0x%x, ps %d, cflags 0x%x\n",
6766 			    service->getName(), service->getRegistryEntryID(),
6767 			    _pendingCapability, powerState, changeFlags);
6768 		}
6769 	}
6770 
6771 	if (actions->state & kPMActionsStatePowerClamped) {
6772 		uint32_t maxPowerState = 0;
6773 
6774 		// Determine the max power state allowed when clamp is enabled
6775 		if (changeFlags & (kIOPMDomainDidChange | kIOPMDomainWillChange)) {
6776 			// Parent intiated power state changes
6777 			if ((service->getPowerState() > maxPowerState) &&
6778 			    (actionFlags & kPMActionsFlagIsDisplayWrangler)) {
6779 				maxPowerState++;
6780 
6781 				// Remove lingering effects of any tickle before entering
6782 				// dark wake. It will take a new tickle to return to full
6783 				// wake, so the existing tickle state is useless.
6784 
6785 				if (changeFlags & kIOPMDomainDidChange) {
6786 					*inOutChangeFlags |= kIOPMExpireIdleTimer;
6787 				}
6788 			} else if (actionFlags & kPMActionsFlagIsGraphicsDriver) {
6789 				maxPowerState++;
6790 			} else if (actionFlags & kPMActionsFlagHasDarkWakePowerState) {
6791 				maxPowerState = actions->darkWakePowerState;
6792 			}
6793 		} else {
6794 			// Deny all self-initiated changes when power is limited.
6795 			// Wrangler tickle should never defeat the limiter.
6796 			maxPowerState = service->getPowerState();
6797 		}
6798 
6799 		if (powerState > maxPowerState) {
6800 			DLOG("power clamped %s %qx, ps %u->%u, cflags 0x%x)\n",
6801 			    service->getName(), service->getRegistryEntryID(),
6802 			    powerState, maxPowerState, changeFlags);
6803 			*inOutPowerState = maxPowerState;
6804 
6805 			if (darkWakePostTickle &&
6806 			    (actionFlags & kPMActionsFlagIsDisplayWrangler) &&
6807 			    (changeFlags & kIOPMDomainWillChange) &&
6808 			    ((gDarkWakeFlags & kDarkWakeFlagPromotionMask) ==
6809 			    kDarkWakeFlagPromotionEarly)) {
6810 				darkWakePostTickle = false;
6811 				reportUserInput();
6812 			}
6813 		}
6814 
6815 		if (!darkWakePowerClamped && (changeFlags & kIOPMDomainDidChange)) {
6816 			if (darkWakeLogClamp) {
6817 				AbsoluteTime    now;
6818 				uint64_t        nsec;
6819 
6820 				clock_get_uptime(&now);
6821 				SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
6822 				absolutetime_to_nanoseconds(now, &nsec);
6823 				DLOG("dark wake power clamped after %u ms\n",
6824 				    ((int)((nsec) / NSEC_PER_MSEC)));
6825 			}
6826 			darkWakePowerClamped = true;
6827 		}
6828 	}
6829 }
6830 
6831 void
6832 IOPMrootDomain::handleActivityTickleForDisplayWrangler(
6833 	IOService *     service,
6834 	IOPMActions *   actions )
6835 {
6836 #if DISPLAY_WRANGLER_PRESENT
6837 	// Warning: Not running in PM work loop context - don't modify state !!!
6838 	// Trap tickle directed to IODisplayWrangler while running with graphics
6839 	// capability suppressed.
6840 
6841 	assert(service == wrangler);
6842 
6843 	clock_get_uptime(&userActivityTime);
6844 	bool aborting = ((lastSleepReason == kIOPMSleepReasonIdle)
6845 	    || (lastSleepReason == kIOPMSleepReasonMaintenance)
6846 	    || (lastSleepReason == kIOPMSleepReasonSoftware));
6847 	if (aborting) {
6848 		userActivityCount++;
6849 		DLOG("display wrangler tickled1 %d lastSleepReason %d\n",
6850 		    userActivityCount, lastSleepReason);
6851 	}
6852 
6853 	if (!darkWakeExit && ((_pendingCapability & kIOPMSystemCapabilityGraphics) == 0)) {
6854 		DLOG("display wrangler tickled\n");
6855 		if (kIOLogPMRootDomain & gIOKitDebug) {
6856 			OSReportWithBacktrace("Dark wake display tickle");
6857 		}
6858 		if (pmPowerStateQueue) {
6859 			pmPowerStateQueue->submitPowerEvent(
6860 				kPowerEventPolicyStimulus,
6861 				(void *) kStimulusDarkWakeActivityTickle,
6862 				true /* set wake type */ );
6863 		}
6864 	}
6865 #endif /* DISPLAY_WRANGLER_PRESENT */
6866 }
6867 
6868 void
6869 IOPMrootDomain::handleUpdatePowerClientForDisplayWrangler(
6870 	IOService *             service,
6871 	IOPMActions *           actions,
6872 	const OSSymbol *        powerClient,
6873 	IOPMPowerStateIndex     oldPowerState,
6874 	IOPMPowerStateIndex     newPowerState )
6875 {
6876 #if DISPLAY_WRANGLER_PRESENT
6877 	assert(service == wrangler);
6878 
6879 	// This function implements half of the user active detection
6880 	// by monitoring changes to the display wrangler's device desire.
6881 	//
6882 	// User becomes active when either:
6883 	// 1. Wrangler's DeviceDesire increases to max, but wrangler is already
6884 	//    in max power state. This desire change in absence of a power state
6885 	//    change is detected within. This handles the case when user becomes
6886 	//    active while the display is already lit by setDisplayPowerOn().
6887 	//
6888 	// 2. Power state change to max, and DeviceDesire is also at max.
6889 	//    Handled by displayWranglerNotification().
6890 	//
6891 	// User becomes inactive when DeviceDesire drops to sleep state or below.
6892 
6893 	DLOG("wrangler %s (ps %u, %u->%u)\n",
6894 	    powerClient->getCStringNoCopy(),
6895 	    (uint32_t) service->getPowerState(),
6896 	    (uint32_t) oldPowerState, (uint32_t) newPowerState);
6897 
6898 	if (powerClient == gIOPMPowerClientDevice) {
6899 		if ((newPowerState > oldPowerState) &&
6900 		    (newPowerState == kWranglerPowerStateMax) &&
6901 		    (service->getPowerState() == kWranglerPowerStateMax)) {
6902 			evaluatePolicy( kStimulusEnterUserActiveState );
6903 		} else if ((newPowerState < oldPowerState) &&
6904 		    (newPowerState <= kWranglerPowerStateSleep)) {
6905 			evaluatePolicy( kStimulusLeaveUserActiveState );
6906 		}
6907 	}
6908 
6909 	if (newPowerState <= kWranglerPowerStateSleep) {
6910 		evaluatePolicy( kStimulusDisplayWranglerSleep );
6911 	} else if (newPowerState == kWranglerPowerStateMax) {
6912 		evaluatePolicy( kStimulusDisplayWranglerWake );
6913 	}
6914 #endif /* DISPLAY_WRANGLER_PRESENT */
6915 }
6916 
6917 //******************************************************************************
6918 // User active state management
6919 //******************************************************************************
6920 
6921 void
6922 IOPMrootDomain::preventTransitionToUserActive( bool prevent )
6923 {
6924 #if DISPLAY_WRANGLER_PRESENT
6925 	_preventUserActive = prevent;
6926 	if (wrangler && !_preventUserActive) {
6927 		// Allowing transition to user active, but the wrangler may have
6928 		// already powered ON in case of sleep cancel/revert. Poll the
6929 		// same conditions checked for in displayWranglerNotification()
6930 		// to bring the user active state up to date.
6931 
6932 		if ((wrangler->getPowerState() == kWranglerPowerStateMax) &&
6933 		    (wrangler->getPowerStateForClient(gIOPMPowerClientDevice) ==
6934 		    kWranglerPowerStateMax)) {
6935 			evaluatePolicy( kStimulusEnterUserActiveState );
6936 		}
6937 	}
6938 #endif /* DISPLAY_WRANGLER_PRESENT */
6939 }
6940 
6941 //******************************************************************************
6942 // Approve usage of delayed child notification by PM.
6943 //******************************************************************************
6944 
6945 bool
6946 IOPMrootDomain::shouldDelayChildNotification(
6947 	IOService * service )
6948 {
6949 	if ((kFullWakeReasonNone == fullWakeReason) &&
6950 	    (kSystemTransitionWake == _systemTransitionType)) {
6951 		DLOG("%s: delay child notify\n", service->getName());
6952 		return true;
6953 	}
6954 	return false;
6955 }
6956 
6957 //******************************************************************************
6958 // PM actions for PCI device.
6959 //******************************************************************************
6960 
6961 void
6962 IOPMrootDomain::handlePowerChangeStartForPCIDevice(
6963 	IOService *             service,
6964 	IOPMActions *           actions,
6965 	const IOPMRequest *     request,
6966 	IOPMPowerStateIndex     powerState,
6967 	IOPMPowerChangeFlags *  inOutChangeFlags )
6968 {
6969 	pmTracer->tracePCIPowerChange(
6970 		PMTraceWorker::kPowerChangeStart,
6971 		service, *inOutChangeFlags,
6972 		(actions->flags & kPMActionsPCIBitNumberMask));
6973 }
6974 
6975 void
6976 IOPMrootDomain::handlePowerChangeDoneForPCIDevice(
6977 	IOService *             service,
6978 	IOPMActions *           actions,
6979 	const IOPMRequest *     request,
6980 	IOPMPowerStateIndex     powerState,
6981 	IOPMPowerChangeFlags    changeFlags )
6982 {
6983 	pmTracer->tracePCIPowerChange(
6984 		PMTraceWorker::kPowerChangeCompleted,
6985 		service, changeFlags,
6986 		(actions->flags & kPMActionsPCIBitNumberMask));
6987 }
6988 
6989 //******************************************************************************
6990 // registerInterest
6991 //
6992 // Override IOService::registerInterest() for root domain clients.
6993 //******************************************************************************
6994 
6995 class IOPMServiceInterestNotifier : public _IOServiceInterestNotifier
6996 {
6997 	friend class IOPMrootDomain;
6998 	OSDeclareDefaultStructors(IOPMServiceInterestNotifier);
6999 
7000 protected:
7001 	uint32_t        ackTimeoutCnt;
7002 	uint32_t        msgType;    // Last type seen by the message filter
7003 	uint32_t        lastSleepWakeMsgType;
7004 	uint32_t        msgIndex;
7005 	uint32_t        maxMsgDelayMS;
7006 	uint32_t        maxAckDelayMS;
7007 	uint64_t        msgAbsTime;
7008 	uint64_t        uuid0;
7009 	uint64_t        uuid1;
7010 	OSSharedPtr<const OSSymbol> identifier;
7011 	OSSharedPtr<const OSSymbol> clientName;
7012 };
7013 
7014 OSDefineMetaClassAndStructors(IOPMServiceInterestNotifier, _IOServiceInterestNotifier)
7015 
7016 OSSharedPtr<IONotifier>
7017 IOPMrootDomain::registerInterest(
7018 	const OSSymbol * typeOfInterest,
7019 	IOServiceInterestHandler handler,
7020 	void * target, void * ref )
7021 {
7022 	IOPMServiceInterestNotifier* notifier;
7023 	bool            isSystemCapabilityClient;
7024 	bool            isKernelCapabilityClient;
7025 	IOReturn        rc = kIOReturnError;
7026 
7027 	isSystemCapabilityClient = typeOfInterest &&
7028 	    typeOfInterest->isEqualTo(kIOPMSystemCapabilityInterest);
7029 
7030 	isKernelCapabilityClient = typeOfInterest &&
7031 	    typeOfInterest->isEqualTo(gIOPriorityPowerStateInterest);
7032 
7033 	if (isSystemCapabilityClient) {
7034 		typeOfInterest = gIOAppPowerStateInterest;
7035 	}
7036 
7037 	notifier = new IOPMServiceInterestNotifier;
7038 	if (!notifier) {
7039 		return NULL;
7040 	}
7041 
7042 	if (notifier->init()) {
7043 		rc  = super::registerInterestForNotifier(notifier, typeOfInterest, handler, target, ref);
7044 	}
7045 	if (rc != kIOReturnSuccess) {
7046 		OSSafeReleaseNULL(notifier);
7047 		return NULL;
7048 	}
7049 
7050 	notifier->ackTimeoutCnt = 0;
7051 
7052 	if (pmPowerStateQueue) {
7053 		if (isSystemCapabilityClient) {
7054 			notifier->retain();
7055 			if (pmPowerStateQueue->submitPowerEvent(
7056 				    kPowerEventRegisterSystemCapabilityClient, notifier) == false) {
7057 				notifier->release();
7058 			}
7059 		}
7060 
7061 		if (isKernelCapabilityClient) {
7062 			notifier->retain();
7063 			if (pmPowerStateQueue->submitPowerEvent(
7064 				    kPowerEventRegisterKernelCapabilityClient, notifier) == false) {
7065 				notifier->release();
7066 			}
7067 		}
7068 	}
7069 
7070 	OSSharedPtr<OSData> data;
7071 	uint8_t *uuid = NULL;
7072 	OSSharedPtr<OSKext> kext = OSKext::lookupKextWithAddress((vm_address_t)handler);
7073 	if (kext) {
7074 		data = kext->copyUUID();
7075 	}
7076 	if (data && (data->getLength() == sizeof(uuid_t))) {
7077 		uuid = (uint8_t *)(data->getBytesNoCopy());
7078 
7079 		notifier->uuid0 = ((uint64_t)(uuid[0]) << 56) | ((uint64_t)(uuid[1]) << 48) | ((uint64_t)(uuid[2]) << 40) |
7080 		    ((uint64_t)(uuid[3]) << 32) | ((uint64_t)(uuid[4]) << 24) | ((uint64_t)(uuid[5]) << 16) |
7081 		    ((uint64_t)(uuid[6]) << 8) | (uuid[7]);
7082 		notifier->uuid1 = ((uint64_t)(uuid[8]) << 56) | ((uint64_t)(uuid[9]) << 48) | ((uint64_t)(uuid[10]) << 40) |
7083 		    ((uint64_t)(uuid[11]) << 32) | ((uint64_t)(uuid[12]) << 24) | ((uint64_t)(uuid[13]) << 16) |
7084 		    ((uint64_t)(uuid[14]) << 8) | (uuid[15]);
7085 
7086 		notifier->identifier = copyKextIdentifierWithAddress((vm_address_t) handler);
7087 	}
7088 	return OSSharedPtr<IOPMServiceInterestNotifier>(notifier, OSNoRetain);
7089 }
7090 
7091 //******************************************************************************
7092 // systemMessageFilter
7093 //
7094 //******************************************************************************
7095 
7096 bool
7097 IOPMrootDomain::systemMessageFilter(
7098 	void * object, void * arg1, void * arg2, void * arg3 )
7099 {
7100 	const IOPMInterestContext * context = (const IOPMInterestContext *) arg1;
7101 	bool  isCapMsg = (context->messageType == kIOMessageSystemCapabilityChange);
7102 	bool  isCapPowerd = (object == (void *) systemCapabilityNotifier.get());
7103 	bool  isCapClient = false;
7104 	bool  allow = false;
7105 	OSBoolean **waitForReply = (typeof(waitForReply))arg3;
7106 	IOPMServiceInterestNotifier *notifier;
7107 
7108 	notifier = OSDynamicCast(IOPMServiceInterestNotifier, (OSObject *)object);
7109 
7110 	do {
7111 		// When powerd and kernel priority clients register capability interest,
7112 		// the power tree is sync'ed to inform those clients about the current
7113 		// system capability. Only allow capability change messages during sync.
7114 		if ((kSystemTransitionNewCapClient == _systemTransitionType) &&
7115 		    (!isCapMsg || !_joinedCapabilityClients ||
7116 		    !_joinedCapabilityClients->containsObject((OSObject *) object))) {
7117 			break;
7118 		}
7119 
7120 		// Capability change message for powerd and kernel clients
7121 		if (isCapMsg) {
7122 			// Kernel priority clients
7123 			if ((context->notifyType == kNotifyPriority) ||
7124 			    (context->notifyType == kNotifyCapabilityChangePriority)) {
7125 				isCapClient = true;
7126 			}
7127 
7128 			// powerd will maintain two client registrations with root domain.
7129 			// isCapPowerd will be TRUE for any message targeting the powerd
7130 			// exclusive (capability change) interest registration.
7131 			if (isCapPowerd && (context->notifyType == kNotifyCapabilityChangeApps)) {
7132 				isCapClient = true;
7133 			}
7134 		}
7135 
7136 		if (isCapClient) {
7137 			IOPMSystemCapabilityChangeParameters * capArgs =
7138 			    (IOPMSystemCapabilityChangeParameters *) arg2;
7139 
7140 			if (kSystemTransitionNewCapClient == _systemTransitionType) {
7141 				capArgs->fromCapabilities = 0;
7142 				capArgs->toCapabilities = _currentCapability;
7143 				capArgs->changeFlags = 0;
7144 			} else {
7145 				capArgs->fromCapabilities = _currentCapability;
7146 				capArgs->toCapabilities = _pendingCapability;
7147 
7148 				if (context->isPreChange) {
7149 					capArgs->changeFlags = kIOPMSystemCapabilityWillChange;
7150 				} else {
7151 					capArgs->changeFlags = kIOPMSystemCapabilityDidChange;
7152 				}
7153 
7154 				if (isCapPowerd && context->isPreChange) {
7155 					toldPowerdCapWillChange = true;
7156 				}
7157 			}
7158 
7159 			// App level capability change messages must only go to powerd.
7160 			// Wait for response post-change if capabilitiy is increasing.
7161 			// Wait for response pre-change if capability is decreasing.
7162 
7163 			if ((context->notifyType == kNotifyCapabilityChangeApps) && waitForReply &&
7164 			    ((capabilityLoss && context->isPreChange) ||
7165 			    (!capabilityLoss && !context->isPreChange))) {
7166 				*waitForReply = kOSBooleanTrue;
7167 			}
7168 
7169 			allow = true;
7170 			break;
7171 		}
7172 
7173 		// powerd will always receive CanSystemSleep, even for a demand sleep.
7174 		// It will also have a final chance to veto sleep after all clients
7175 		// have responded to SystemWillSleep
7176 
7177 		if ((kIOMessageCanSystemSleep == context->messageType) ||
7178 		    (kIOMessageSystemWillNotSleep == context->messageType)) {
7179 			if (isCapPowerd) {
7180 				allow = true;
7181 				break;
7182 			}
7183 
7184 			// Demand sleep, don't ask apps for permission
7185 			if (context->changeFlags & kIOPMSkipAskPowerDown) {
7186 				break;
7187 			}
7188 		}
7189 
7190 		if (kIOPMMessageLastCallBeforeSleep == context->messageType) {
7191 			if (isCapPowerd && CAP_HIGHEST(kIOPMSystemCapabilityGraphics) &&
7192 			    (fullToDarkReason == kIOPMSleepReasonIdle)) {
7193 				allow = true;
7194 			}
7195 			break;
7196 		}
7197 
7198 		// Drop capability change messages for legacy clients.
7199 		// Drop legacy system sleep messages for powerd capability interest.
7200 		if (isCapMsg || isCapPowerd) {
7201 			break;
7202 		}
7203 
7204 		// Not a capability change message.
7205 		// Perform message filtering based on _systemMessageClientMask.
7206 
7207 		if ((context->notifyType == kNotifyApps) &&
7208 		    (_systemMessageClientMask & kSystemMessageClientLegacyApp)) {
7209 			if (!notifier) {
7210 				break;
7211 			}
7212 
7213 			if ((notifier->lastSleepWakeMsgType == context->messageType) &&
7214 			    (notifier->lastSleepWakeMsgType == kIOMessageSystemWillPowerOn)) {
7215 				break; // drop any duplicate WillPowerOn for AOT devices
7216 			}
7217 
7218 			allow = true;
7219 
7220 			if (waitForReply) {
7221 				if (notifier->ackTimeoutCnt >= 3) {
7222 					*waitForReply = kOSBooleanFalse;
7223 				} else {
7224 					*waitForReply = kOSBooleanTrue;
7225 				}
7226 			}
7227 		} else if ((context->notifyType == kNotifyPriority) &&
7228 		    (_systemMessageClientMask & kSystemMessageClientKernel)) {
7229 			allow = true;
7230 		}
7231 
7232 		// Check sleep/wake message ordering
7233 		if (allow) {
7234 			if (context->messageType == kIOMessageSystemWillSleep ||
7235 			    context->messageType == kIOMessageSystemWillPowerOn ||
7236 			    context->messageType == kIOMessageSystemHasPoweredOn) {
7237 				notifier->lastSleepWakeMsgType = context->messageType;
7238 			}
7239 		}
7240 	} while (false);
7241 
7242 	if (allow && isCapMsg && _joinedCapabilityClients) {
7243 		_joinedCapabilityClients->removeObject((OSObject *) object);
7244 		if (_joinedCapabilityClients->getCount() == 0) {
7245 			DMSG("destroyed capability client set %p\n",
7246 			    OBFUSCATE(_joinedCapabilityClients.get()));
7247 			_joinedCapabilityClients.reset();
7248 		}
7249 	}
7250 	if (notifier) {
7251 		// Record the last seen message type even if the message is dropped
7252 		// for traceFilteredNotification().
7253 		notifier->msgType = context->messageType;
7254 	}
7255 
7256 	return allow;
7257 }
7258 
7259 //******************************************************************************
7260 // setMaintenanceWakeCalendar
7261 //
7262 //******************************************************************************
7263 
7264 IOReturn
7265 IOPMrootDomain::setMaintenanceWakeCalendar(
7266 	const IOPMCalendarStruct * calendar )
7267 {
7268 	OSSharedPtr<OSData> data;
7269 	IOReturn ret = 0;
7270 
7271 	if (!calendar) {
7272 		return kIOReturnBadArgument;
7273 	}
7274 
7275 	data = OSData::withValue(*calendar);
7276 	if (!data) {
7277 		return kIOReturnNoMemory;
7278 	}
7279 
7280 	if (kPMCalendarTypeMaintenance == calendar->selector) {
7281 		ret = setPMSetting(gIOPMSettingMaintenanceWakeCalendarKey.get(), data.get());
7282 	} else if (kPMCalendarTypeSleepService == calendar->selector) {
7283 		ret = setPMSetting(gIOPMSettingSleepServiceWakeCalendarKey.get(), data.get());
7284 	}
7285 
7286 	return ret;
7287 }
7288 
7289 // MARK: -
7290 // MARK: Display Wrangler
7291 
7292 //******************************************************************************
7293 // displayWranglerNotification
7294 //
7295 // Handle the notification when the IODisplayWrangler changes power state.
7296 //******************************************************************************
7297 
7298 IOReturn
7299 IOPMrootDomain::displayWranglerNotification(
7300 	void * target, void * refCon,
7301 	UInt32 messageType, IOService * service,
7302 	void * messageArgument, vm_size_t argSize )
7303 {
7304 #if DISPLAY_WRANGLER_PRESENT
7305 	IOPMPowerStateIndex                 displayPowerState;
7306 	IOPowerStateChangeNotification *    params =
7307 	    (IOPowerStateChangeNotification *) messageArgument;
7308 
7309 	if ((messageType != kIOMessageDeviceWillPowerOff) &&
7310 	    (messageType != kIOMessageDeviceHasPoweredOn)) {
7311 		return kIOReturnUnsupported;
7312 	}
7313 
7314 	ASSERT_GATED();
7315 	if (!gRootDomain) {
7316 		return kIOReturnUnsupported;
7317 	}
7318 
7319 	displayPowerState = params->stateNumber;
7320 	DLOG("wrangler %s ps %d\n",
7321 	    getIOMessageString(messageType), (uint32_t) displayPowerState);
7322 
7323 	switch (messageType) {
7324 	case kIOMessageDeviceWillPowerOff:
7325 		// Display wrangler has dropped power due to display idle
7326 		// or force system sleep.
7327 		//
7328 		// 4 Display ON             kWranglerPowerStateMax
7329 		// 3 Display Dim            kWranglerPowerStateDim
7330 		// 2 Display Sleep          kWranglerPowerStateSleep
7331 		// 1 Not visible to user
7332 		// 0 Not visible to user    kWranglerPowerStateMin
7333 
7334 		if (displayPowerState <= kWranglerPowerStateSleep) {
7335 			gRootDomain->evaluatePolicy( kStimulusDisplayWranglerSleep );
7336 		}
7337 		break;
7338 
7339 	case kIOMessageDeviceHasPoweredOn:
7340 		// Display wrangler has powered on due to user activity
7341 		// or wake from sleep.
7342 
7343 		if (kWranglerPowerStateMax == displayPowerState) {
7344 			gRootDomain->evaluatePolicy( kStimulusDisplayWranglerWake );
7345 
7346 			// See comment in handleUpdatePowerClientForDisplayWrangler
7347 			if (service->getPowerStateForClient(gIOPMPowerClientDevice) ==
7348 			    kWranglerPowerStateMax) {
7349 				gRootDomain->evaluatePolicy( kStimulusEnterUserActiveState );
7350 			}
7351 		}
7352 		break;
7353 	}
7354 #endif /* DISPLAY_WRANGLER_PRESENT */
7355 	return kIOReturnUnsupported;
7356 }
7357 
7358 //******************************************************************************
7359 // reportUserInput
7360 //
7361 //******************************************************************************
7362 
7363 void
7364 IOPMrootDomain::updateUserActivity( void )
7365 {
7366 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
7367 	clock_get_uptime(&userActivityTime);
7368 	bool aborting =  ((lastSleepReason == kIOPMSleepReasonSoftware)
7369 	    || (lastSleepReason == kIOPMSleepReasonIdle)
7370 	    || (lastSleepReason == kIOPMSleepReasonMaintenance));
7371 	if (aborting) {
7372 		userActivityCount++;
7373 		DLOG("user activity reported %d lastSleepReason %d\n", userActivityCount, lastSleepReason);
7374 	}
7375 #endif
7376 }
7377 void
7378 IOPMrootDomain::reportUserInput( void )
7379 {
7380 	if (wrangler) {
7381 		wrangler->activityTickle(0, 0);
7382 	}
7383 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
7384 	// Update user activity
7385 	updateUserActivity();
7386 
7387 	if (!darkWakeExit && ((_pendingCapability & kIOPMSystemCapabilityGraphics) == 0)) {
7388 		// update user active abs time
7389 		clock_get_uptime(&gUserActiveAbsTime);
7390 		pmPowerStateQueue->submitPowerEvent(
7391 			kPowerEventPolicyStimulus,
7392 			(void *) kStimulusDarkWakeActivityTickle,
7393 			true /* set wake type */ );
7394 	}
7395 #endif
7396 }
7397 
7398 void
7399 IOPMrootDomain::requestUserActive(IOService *device, const char *reason)
7400 {
7401 #if DISPLAY_WRANGLER_PRESENT
7402 	if (wrangler) {
7403 		wrangler->activityTickle(0, 0);
7404 	}
7405 #else
7406 	if (!device) {
7407 		DLOG("requestUserActive: device is null\n");
7408 		return;
7409 	}
7410 	OSSharedPtr<const OSSymbol> deviceName = device->copyName();
7411 	uint64_t registryID = device->getRegistryEntryID();
7412 
7413 	if (!deviceName || !registryID) {
7414 		DLOG("requestUserActive: no device name or registry entry\n");
7415 		return;
7416 	}
7417 	const char *name = deviceName->getCStringNoCopy();
7418 	char payload[128];
7419 	snprintf(payload, sizeof(payload), "%s:%s", name, reason);
7420 	DLOG("requestUserActive from %s (0x%llx) for %s\n", name, registryID, reason);
7421 	messageClient(kIOPMMessageRequestUserActive, systemCapabilityNotifier.get(), (void *)payload, sizeof(payload));
7422 #endif
7423 }
7424 
7425 //******************************************************************************
7426 // latchDisplayWranglerTickle
7427 //******************************************************************************
7428 
7429 bool
7430 IOPMrootDomain::latchDisplayWranglerTickle( bool latch )
7431 {
7432 #if DISPLAY_WRANGLER_PRESENT
7433 	if (latch) {
7434 		if (!(_currentCapability & kIOPMSystemCapabilityGraphics) &&
7435 		    !(_pendingCapability & kIOPMSystemCapabilityGraphics) &&
7436 		    !checkSystemCanSustainFullWake()) {
7437 			// Currently in dark wake, and not transitioning to full wake.
7438 			// Full wake is unsustainable, so latch the tickle to prevent
7439 			// the display from lighting up momentarily.
7440 			wranglerTickled = true;
7441 		} else {
7442 			wranglerTickled = false;
7443 		}
7444 	} else if (wranglerTickled && checkSystemCanSustainFullWake()) {
7445 		wranglerTickled = false;
7446 
7447 		pmPowerStateQueue->submitPowerEvent(
7448 			kPowerEventPolicyStimulus,
7449 			(void *) kStimulusDarkWakeActivityTickle );
7450 	}
7451 
7452 	return wranglerTickled;
7453 #else  /* ! DISPLAY_WRANGLER_PRESENT */
7454 	return false;
7455 #endif /* ! DISPLAY_WRANGLER_PRESENT */
7456 }
7457 
7458 //******************************************************************************
7459 // setDisplayPowerOn
7460 //
7461 // For root domain user client
7462 //******************************************************************************
7463 
7464 void
7465 IOPMrootDomain::setDisplayPowerOn( uint32_t options )
7466 {
7467 	pmPowerStateQueue->submitPowerEvent( kPowerEventSetDisplayPowerOn,
7468 	    (void *) NULL, options );
7469 }
7470 
7471 // MARK: -
7472 // MARK: System PM Policy
7473 
7474 //******************************************************************************
7475 // checkSystemSleepAllowed
7476 //
7477 //******************************************************************************
7478 
7479 bool
7480 IOPMrootDomain::checkSystemSleepAllowed( IOOptionBits options,
7481     uint32_t     sleepReason )
7482 {
7483 	uint32_t err = 0;
7484 
7485 	// Conditions that prevent idle and demand system sleep.
7486 
7487 	do {
7488 		if (gSleepDisabledFlag) {
7489 			err = kPMConfigPreventSystemSleep;
7490 			break;
7491 		}
7492 
7493 		if (userDisabledAllSleep) {
7494 			err = kPMUserDisabledAllSleep; // 1. user-space sleep kill switch
7495 			break;
7496 		}
7497 
7498 		if (systemBooting || systemShutdown || gWillShutdown) {
7499 			err = kPMSystemRestartBootingInProgress; // 2. restart or shutdown in progress
7500 			break;
7501 		}
7502 
7503 		if (options == 0) {
7504 			break;
7505 		}
7506 
7507 		// Conditions above pegs the system at full wake.
7508 		// Conditions below prevent system sleep but does not prevent
7509 		// dark wake, and must be called from gated context.
7510 
7511 #if !CONFIG_SLEEP
7512 		err = kPMConfigPreventSystemSleep;    // 3. config does not support sleep
7513 		break;
7514 #endif
7515 
7516 		if (lowBatteryCondition || thermalWarningState || thermalEmergencyState) {
7517 			break; // always sleep on low battery or when in thermal warning/emergency state
7518 		}
7519 
7520 		if (sleepReason == kIOPMSleepReasonDarkWakeThermalEmergency) {
7521 			break; // always sleep on dark wake thermal emergencies
7522 		}
7523 
7524 		if (preventSystemSleepList->getCount() != 0) {
7525 			err = kPMChildPreventSystemSleep; // 4. child prevent system sleep clamp
7526 			break;
7527 		}
7528 
7529 		if (_driverKitMatchingAssertionCount != 0) {
7530 			err = kPMCPUAssertion;
7531 			break;
7532 		}
7533 
7534 		// Check for any dexts currently being added to the PM tree. Sleeping while
7535 		// this is in flight can cause IOServicePH to timeout.
7536 		if (!IOServicePH::checkPMReady()) {
7537 #if !defined(XNU_TARGET_OS_OSX)
7538 			// 116893363: kPMDKNotReady sleep cancellations often leaves embedded devices
7539 			// in dark wake for long periods of time, which causes issues as apps were
7540 			// already informed of sleep during the f->9 transition. As a temporary
7541 			// measure, always full wake if we hit this specific condition.
7542 			pmPowerStateQueue->submitPowerEvent(
7543 				kPowerEventPolicyStimulus,
7544 				(void *) kStimulusDarkWakeActivityTickle);
7545 #endif
7546 			err = kPMDKNotReady;
7547 			break;
7548 		}
7549 
7550 		if (getPMAssertionLevel( kIOPMDriverAssertionCPUBit ) ==
7551 		    kIOPMDriverAssertionLevelOn) {
7552 			err = kPMCPUAssertion; // 5. CPU assertion
7553 			break;
7554 		}
7555 
7556 		if (pciCantSleepValid) {
7557 			if (pciCantSleepFlag) {
7558 				err = kPMPCIUnsupported; // 6. PCI card does not support PM (cached)
7559 			}
7560 			break;
7561 		} else if (sleepSupportedPEFunction &&
7562 		    CAP_HIGHEST(kIOPMSystemCapabilityGraphics)) {
7563 			IOReturn ret;
7564 			OSBitAndAtomic(~kPCICantSleep, &platformSleepSupport);
7565 			ret = getPlatform()->callPlatformFunction(
7566 				sleepSupportedPEFunction.get(), false,
7567 				NULL, NULL, NULL, NULL);
7568 			pciCantSleepValid = true;
7569 			pciCantSleepFlag  = false;
7570 			if ((platformSleepSupport & kPCICantSleep) ||
7571 			    ((ret != kIOReturnSuccess) && (ret != kIOReturnUnsupported))) {
7572 				err = 6; // 6. PCI card does not support PM
7573 				pciCantSleepFlag = true;
7574 				break;
7575 			}
7576 		}
7577 	}while (false);
7578 
7579 	if (err) {
7580 		DLOG("System sleep prevented by %s\n", getSystemSleepPreventerString(err));
7581 		return false;
7582 	}
7583 	return true;
7584 }
7585 
7586 bool
7587 IOPMrootDomain::checkSystemSleepEnabled( void )
7588 {
7589 	return checkSystemSleepAllowed(0, 0);
7590 }
7591 
7592 bool
7593 IOPMrootDomain::checkSystemCanSleep( uint32_t sleepReason )
7594 {
7595 	ASSERT_GATED();
7596 	return checkSystemSleepAllowed(1, sleepReason);
7597 }
7598 
7599 //******************************************************************************
7600 // checkSystemCanSustainFullWake
7601 //******************************************************************************
7602 
7603 bool
7604 IOPMrootDomain::checkSystemCanSustainFullWake( void )
7605 {
7606 	if (lowBatteryCondition || thermalWarningState || thermalEmergencyState) {
7607 		// Low battery wake, or received a low battery notification
7608 		// while system is awake. This condition will persist until
7609 		// the following wake.
7610 		return false;
7611 	}
7612 
7613 	if (clamshellExists && clamshellClosed && !clamshellSleepDisableMask) {
7614 		// Graphics state is unknown and external display might not be probed.
7615 		// Do not incorporate state that requires graphics to be in max power
7616 		// such as desktopMode or clamshellDisabled.
7617 
7618 		if (!acAdaptorConnected) {
7619 			DLOG("full wake check: no AC\n");
7620 			return false;
7621 		}
7622 	}
7623 	return true;
7624 }
7625 
7626 //******************************************************************************
7627 // checkSystemCanAbortIdleSleep
7628 //******************************************************************************
7629 
7630 bool
7631 IOPMrootDomain::checkSystemCanAbortIdleSleep( void )
7632 {
7633 	bool abortableSleepType =  ((lastSleepReason == kIOPMSleepReasonIdle)
7634 	    || (lastSleepReason == 0));
7635 	return idleSleepRevertible && abortableSleepType;
7636 }
7637 
7638 //******************************************************************************
7639 // attemptIdleSleepAbort
7640 //******************************************************************************
7641 
7642 bool
7643 IOPMrootDomain::attemptIdleSleepAbort( void )
7644 {
7645 	if (!gIOPMWorkLoop->inGate()) {
7646 		bool ret = gIOPMWorkLoop->runAction(
7647 			OSMemberFunctionCast(IOWorkLoop::Action, this,
7648 			&IOPMrootDomain::attemptIdleSleepAbort),
7649 			this);
7650 		return ret;
7651 	}
7652 
7653 	bool canAbort = checkSystemCanAbortIdleSleep();
7654 	if (canAbort) {
7655 		cancelIdlePowerDownSync();
7656 	} else if (lastSleepReason == kIOPMSleepReasonIdle) {
7657 		scheduleImmediateDebugWake();
7658 	}
7659 
7660 	return canAbort;
7661 }
7662 
7663 //******************************************************************************
7664 // setIdleSleepRevertible
7665 //******************************************************************************
7666 
7667 void
7668 IOPMrootDomain::setIdleSleepRevertible( bool revertible )
7669 {
7670 	idleSleepRevertible = revertible;
7671 }
7672 
7673 //******************************************************************************
7674 // mustHibernate
7675 //******************************************************************************
7676 
7677 #if HIBERNATION
7678 
7679 bool
7680 IOPMrootDomain::mustHibernate( void )
7681 {
7682 	return lowBatteryCondition || thermalWarningState;
7683 }
7684 
7685 #endif /* HIBERNATION */
7686 
7687 //******************************************************************************
7688 // AOT
7689 //******************************************************************************
7690 
7691 // Tables for accumulated days in year by month, latter used for leap years
7692 
7693 static const unsigned int daysbymonth[] =
7694 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
7695 
7696 static const unsigned int lydaysbymonth[] =
7697 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 };
7698 
7699 static int __unused
7700 IOPMConvertSecondsToCalendar(clock_sec_t secs, IOPMCalendarStruct * dt)
7701 {
7702 	const unsigned int *    dbm = daysbymonth;
7703 	clock_sec_t             n, x, y, z;
7704 
7705 	// Calculate seconds, minutes and hours
7706 
7707 	n = secs % (24 * 3600);
7708 	dt->second = n % 60;
7709 	n /= 60;
7710 	dt->minute = n % 60;
7711 	dt->hour = (typeof(dt->hour))(n / 60);
7712 
7713 	// Calculate day of week
7714 
7715 	n = secs / (24 * 3600);
7716 //	dt->dayWeek = (n + 4) % 7;
7717 
7718 	// Calculate year
7719 	// Rebase from days since Unix epoch (1/1/1970) store in 'n',
7720 	// to days since 1/1/1968 to start on 4 year cycle, beginning
7721 	// on a leap year.
7722 
7723 	n += (366 + 365);
7724 
7725 	// Every 4 year cycle will be exactly (366 + 365 * 3) = 1461 days.
7726 	// Valid before 2100, since 2100 is not a leap year.
7727 
7728 	x = n / 1461;       // number of 4 year cycles
7729 	y = n % 1461;       // days into current 4 year cycle
7730 	z = 1968 + (4 * x);
7731 
7732 	// Add in years in the current 4 year cycle
7733 
7734 	if (y >= 366) {
7735 		y -= 366;   // days after the leap year
7736 		n = y % 365; // days into the current year
7737 		z += (1 + y / 365); // years after the past 4-yr cycle
7738 	} else {
7739 		n = y;
7740 		dbm = lydaysbymonth;
7741 	}
7742 	if (z > 2099) {
7743 		return 0;
7744 	}
7745 
7746 	dt->year = (typeof(dt->year))z;
7747 
7748 	// Adjust remaining days value to start at 1
7749 
7750 	n += 1;
7751 
7752 	// Calculate month
7753 
7754 	for (x = 1; (n > dbm[x]) && (x < 12); x++) {
7755 		continue;
7756 	}
7757 	dt->month = (typeof(dt->month))x;
7758 
7759 	// Calculate day of month
7760 
7761 	dt->day = (typeof(dt->day))(n - dbm[x - 1]);
7762 
7763 	return 1;
7764 }
7765 
7766 static clock_sec_t
7767 IOPMConvertCalendarToSeconds(const IOPMCalendarStruct * dt)
7768 {
7769 	const unsigned int *    dbm = daysbymonth;
7770 	long                    y, secs, days;
7771 
7772 	if (dt->year < 1970 || dt->month > 12) {
7773 		return 0;
7774 	}
7775 
7776 	// Seconds elapsed in the current day
7777 
7778 	secs = dt->second + 60 * dt->minute + 3600 * dt->hour;
7779 
7780 	// Number of days from 1/1/70 to beginning of current year
7781 	// Account for extra day every 4 years starting at 1973
7782 
7783 	y = dt->year - 1970;
7784 	days = (y * 365) + ((y + 1) / 4);
7785 
7786 	// Change table if current year is a leap year
7787 
7788 	if ((dt->year % 4) == 0) {
7789 		dbm = lydaysbymonth;
7790 	}
7791 
7792 	// Add in days elapsed in the current year
7793 
7794 	days += (dt->day - 1) + dbm[dt->month - 1];
7795 
7796 	// Add accumulated days to accumulated seconds
7797 
7798 	secs += 24 * 3600 * days;
7799 
7800 	return secs;
7801 }
7802 
7803 unsigned long
7804 IOPMrootDomain::getRUN_STATE(void)
7805 {
7806 	return (_aotNow && !(kIOPMWakeEventAOTExitFlags & _aotPendingFlags)) ? AOT_STATE : ON_STATE;
7807 }
7808 
7809 bool
7810 IOPMrootDomain::isAOTMode()
7811 {
7812 	return _aotNow;
7813 }
7814 
7815 IOReturn
7816 IOPMrootDomain::setWakeTime(uint64_t wakeContinuousTime)
7817 {
7818 	clock_sec_t     nowsecs, wakesecs;
7819 	clock_usec_t    nowmicrosecs, wakemicrosecs;
7820 	uint64_t        nowAbs, wakeAbs;
7821 
7822 	if (!_aotMode) {
7823 		return kIOReturnNotReady;
7824 	}
7825 
7826 	clock_gettimeofday_and_absolute_time(&nowsecs, &nowmicrosecs, &nowAbs);
7827 	wakeAbs = continuoustime_to_absolutetime(wakeContinuousTime);
7828 	if (wakeAbs < nowAbs) {
7829 		printf(LOG_PREFIX "wakeAbs %qd < nowAbs %qd\n", wakeAbs, nowAbs);
7830 		wakeAbs = nowAbs;
7831 	}
7832 	wakeAbs -= nowAbs;
7833 	absolutetime_to_microtime(wakeAbs, &wakesecs, &wakemicrosecs);
7834 
7835 	wakesecs += nowsecs;
7836 	wakemicrosecs += nowmicrosecs;
7837 	if (wakemicrosecs >= USEC_PER_SEC) {
7838 		wakesecs++;
7839 		wakemicrosecs -= USEC_PER_SEC;
7840 	}
7841 	if (wakemicrosecs >= (USEC_PER_SEC / 10)) {
7842 		wakesecs++;
7843 	}
7844 
7845 	IOPMConvertSecondsToCalendar(wakesecs, &_aotWakeTimeCalendar);
7846 
7847 	if (_aotWakeTimeContinuous != wakeContinuousTime) {
7848 		_aotWakeTimeContinuous = wakeContinuousTime;
7849 		IOLog(LOG_PREFIX "setWakeTime: " YMDTF "\n", YMDT(&_aotWakeTimeCalendar));
7850 	}
7851 	_aotWakeTimeCalendar.selector = kPMCalendarTypeMaintenance;
7852 	_aotWakeTimeUTC               = wakesecs;
7853 
7854 	return kIOReturnSuccess;
7855 }
7856 
7857 // assumes WAKEEVENT_LOCK
7858 bool
7859 IOPMrootDomain::aotShouldExit(bool software)
7860 {
7861 	bool exitNow = false;
7862 	const char * reason = "";
7863 
7864 	if (!_aotNow) {
7865 		return false;
7866 	}
7867 
7868 	if (software) {
7869 		exitNow = true;
7870 		_aotMetrics->softwareRequestCount++;
7871 		reason = "software request";
7872 	} else if (kIOPMWakeEventAOTExitFlags & _aotPendingFlags) {
7873 		exitNow = true;
7874 		reason = gWakeReasonString;
7875 	} else if ((kIOPMAOTModeRespectTimers & _aotMode) && _calendarWakeAlarmUTC) {
7876 		clock_sec_t     sec;
7877 		clock_usec_t    usec;
7878 		clock_get_calendar_microtime(&sec, &usec);
7879 		if (_calendarWakeAlarmUTC <= sec) {
7880 			exitNow = true;
7881 			_aotMetrics->rtcAlarmsCount++;
7882 			reason = "user alarm";
7883 		}
7884 	}
7885 	if (exitNow) {
7886 		_aotPendingFlags |= kIOPMWakeEventAOTExit;
7887 		IOLog(LOG_PREFIX "AOT exit for %s, sc %d po %d, cp %d, rj %d, ex %d, nt %d, rt %d\n",
7888 		    reason,
7889 		    _aotMetrics->sleepCount,
7890 		    _aotMetrics->possibleCount,
7891 		    _aotMetrics->confirmedPossibleCount,
7892 		    _aotMetrics->rejectedPossibleCount,
7893 		    _aotMetrics->expiredPossibleCount,
7894 		    _aotMetrics->noTimeSetCount,
7895 		    _aotMetrics->rtcAlarmsCount);
7896 	}
7897 	return exitNow;
7898 }
7899 
7900 void
7901 IOPMrootDomain::aotExit(bool cps)
7902 {
7903 	uint32_t savedMessageMask;
7904 
7905 	ASSERT_GATED();
7906 	_aotNow = false;
7907 	_aotReadyToFullWake = false;
7908 	if (_aotTimerScheduled) {
7909 		_aotTimerES->cancelTimeout();
7910 		_aotTimerScheduled = false;
7911 	}
7912 	updateTasksSuspend(kTasksSuspendNoChange, kTasksSuspendUnsuspended);
7913 
7914 	_aotMetrics->totalTime += mach_absolute_time() - _aotLastWakeTime;
7915 	_aotLastWakeTime = 0;
7916 	if (_aotMetrics->sleepCount && (_aotMetrics->sleepCount <= kIOPMAOTMetricsKernelWakeCountMax)) {
7917 		WAKEEVENT_LOCK();
7918 		strlcpy(&_aotMetrics->kernelWakeReason[_aotMetrics->sleepCount - 1][0],
7919 		    gWakeReasonString,
7920 		    sizeof(_aotMetrics->kernelWakeReason[_aotMetrics->sleepCount]));
7921 		WAKEEVENT_UNLOCK();
7922 	}
7923 
7924 	_aotWakeTimeCalendar.selector = kPMCalendarTypeInvalid;
7925 
7926 	// Preserve the message mask since a system wake transition
7927 	// may have already started and initialized the mask.
7928 	savedMessageMask = _systemMessageClientMask;
7929 	_systemMessageClientMask = kSystemMessageClientLegacyApp;
7930 	tellClients(kIOMessageSystemWillPowerOn);
7931 	_systemMessageClientMask = savedMessageMask | kSystemMessageClientLegacyApp;
7932 
7933 	if (cps) {
7934 		changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonAOTExit);
7935 	}
7936 }
7937 
7938 void
7939 IOPMrootDomain::aotEvaluate(IOTimerEventSource * timer)
7940 {
7941 	bool exitNow;
7942 
7943 	IOLog("aotEvaluate(%d) 0x%x\n", (timer != NULL), _aotPendingFlags);
7944 
7945 	WAKEEVENT_LOCK();
7946 	exitNow = aotShouldExit(false);
7947 	if (timer != NULL) {
7948 		_aotTimerScheduled = false;
7949 	}
7950 	WAKEEVENT_UNLOCK();
7951 	if (exitNow) {
7952 		aotExit(true);
7953 	} else {
7954 #if 0
7955 		if (_aotLingerTime) {
7956 			uint64_t deadline;
7957 			IOLog("aot linger before sleep\n");
7958 			clock_absolutetime_interval_to_deadline(_aotLingerTime, &deadline);
7959 			clock_delay_until(deadline);
7960 		}
7961 #endif
7962 		privateSleepSystem(kIOPMSleepReasonSoftware);
7963 	}
7964 }
7965 
7966 //******************************************************************************
7967 // adjustPowerState
7968 //
7969 // Conditions that affect our wake/sleep decision has changed.
7970 // If conditions dictate that the system must remain awake, clamp power
7971 // state to max with changePowerStateToPriv(ON). Otherwise if sleepASAP
7972 // is TRUE, then remove the power clamp and allow the power state to drop
7973 // to SLEEP_STATE.
7974 //******************************************************************************
7975 
7976 void
7977 IOPMrootDomain::adjustPowerState( bool sleepASAP )
7978 {
7979 	DEBUG_LOG("adjustPowerState %s, asap %d, idleSleepEnabled %d\n",
7980 	    getPowerStateString((uint32_t) getPowerState()), sleepASAP, idleSleepEnabled);
7981 
7982 	ASSERT_GATED();
7983 
7984 	if (_aotNow) {
7985 		bool exitNow;
7986 
7987 		if (AOT_STATE != getPowerState()) {
7988 			return;
7989 		}
7990 		WAKEEVENT_LOCK();
7991 		exitNow = aotShouldExit(false);
7992 		if (!exitNow
7993 		    && !_aotTimerScheduled
7994 		    && (kIOPMWakeEventAOTPossibleExit == (kIOPMWakeEventAOTPossibleFlags & _aotPendingFlags))) {
7995 			_aotTimerScheduled = true;
7996 			if (_aotLingerTime) {
7997 				_aotTimerES->setTimeout(_aotLingerTime);
7998 			} else {
7999 				_aotTimerES->setTimeout(800, kMillisecondScale);
8000 			}
8001 		}
8002 		WAKEEVENT_UNLOCK();
8003 		if (exitNow) {
8004 			aotExit(true);
8005 		} else {
8006 			_aotReadyToFullWake = true;
8007 			if (!_aotTimerScheduled) {
8008 				if (kIOPMDriverAssertionLevelOn == getPMAssertionLevel(kIOPMDriverAssertionCPUBit)) {
8009 					// Don't try to force sleep during AOT while IOMobileFramebuffer is holding a power assertion.
8010 					// Doing so will result in the sleep being cancelled anyway,
8011 					// but this check avoids unnecessary thrashing in the power state engine.
8012 					return;
8013 				}
8014 				privateSleepSystem(kIOPMSleepReasonSoftware);
8015 			}
8016 		}
8017 		return;
8018 	}
8019 
8020 	if ((!idleSleepEnabled) || !checkSystemSleepEnabled()) {
8021 		changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonAdjustPowerState);
8022 	} else if (sleepASAP) {
8023 		changePowerStateWithTagToPriv(SLEEP_STATE, kCPSReasonAdjustPowerState);
8024 	}
8025 }
8026 
8027 void
8028 IOPMrootDomain::handleSetDisplayPowerOn(bool powerOn)
8029 {
8030 	if (powerOn) {
8031 		if (!checkSystemCanSustainFullWake()) {
8032 			DLOG("System cannot sustain full wake\n");
8033 			return;
8034 		}
8035 
8036 		// Force wrangler to max power state. If system is in dark wake
8037 		// this alone won't raise the wrangler's power state.
8038 		if (wrangler) {
8039 			wrangler->changePowerStateForRootDomain(kWranglerPowerStateMax);
8040 		}
8041 
8042 		// System in dark wake, always requesting full wake should
8043 		// not have any bad side-effects, even if the request fails.
8044 
8045 		if (!CAP_CURRENT(kIOPMSystemCapabilityGraphics)) {
8046 			setProperty(kIOPMRootDomainWakeTypeKey, kIOPMRootDomainWakeTypeNotification);
8047 			requestFullWake( kFullWakeReasonDisplayOn );
8048 		}
8049 	} else {
8050 		// Relenquish desire to power up display.
8051 		// Must first transition to state 1 since wrangler doesn't
8052 		// power off the displays at state 0. At state 0 the root
8053 		// domain is removed from the wrangler's power client list.
8054 		if (wrangler) {
8055 			wrangler->changePowerStateForRootDomain(kWranglerPowerStateMin + 1);
8056 			wrangler->changePowerStateForRootDomain(kWranglerPowerStateMin);
8057 		}
8058 	}
8059 }
8060 
8061 TUNABLE(bool, test_sleep_in_vm, "test_sleep_in_vm", false);
8062 
8063 //******************************************************************************
8064 // dispatchPowerEvent
8065 //
8066 // IOPMPowerStateQueue callback function. Running on PM work loop thread.
8067 //******************************************************************************
8068 
8069 void
8070 IOPMrootDomain::dispatchPowerEvent(
8071 	uint32_t event, void * arg0, uint64_t arg1 )
8072 {
8073 	ASSERT_GATED();
8074 
8075 	switch (event) {
8076 	case kPowerEventFeatureChanged:
8077 		DMSG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8078 		messageClients(kIOPMMessageFeatureChange, this);
8079 		break;
8080 
8081 	case kPowerEventReceivedPowerNotification:
8082 		DMSG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8083 		handlePowerNotification((UInt32)(uintptr_t) arg0 );
8084 		break;
8085 
8086 	case kPowerEventSystemBootCompleted:
8087 		DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8088 		if (systemBooting) {
8089 			systemBooting = false;
8090 
8091 			if (PE_get_default("sleep-disabled", &gSleepDisabledFlag, sizeof(gSleepDisabledFlag))) {
8092 				DLOG("Setting gSleepDisabledFlag to %u from device tree\n", gSleepDisabledFlag);
8093 				if (test_sleep_in_vm && gSleepDisabledFlag) {
8094 					DLOG("Clearing gSleepDisabledFlag due to test_sleep_in_vm boot-arg\n");
8095 					gSleepDisabledFlag = 0;
8096 				}
8097 			}
8098 
8099 			if (lowBatteryCondition || thermalEmergencyState) {
8100 				if (lowBatteryCondition) {
8101 					privateSleepSystem(kIOPMSleepReasonLowPower);
8102 				} else {
8103 					privateSleepSystem(kIOPMSleepReasonThermalEmergency);
8104 				}
8105 				// The rest is unnecessary since the system is expected
8106 				// to sleep immediately. The following wake will update
8107 				// everything.
8108 				break;
8109 			}
8110 
8111 			sleepWakeDebugMemAlloc();
8112 			saveFailureData2File();
8113 
8114 			// If lid is closed, re-send lid closed notification
8115 			// now that booting is complete.
8116 			if (clamshellClosed) {
8117 				handlePowerNotification(kLocalEvalClamshellCommand);
8118 			}
8119 			evaluatePolicy( kStimulusAllowSystemSleepChanged );
8120 		}
8121 		break;
8122 
8123 	case kPowerEventSystemShutdown:
8124 		DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8125 		if (kOSBooleanTrue == (OSBoolean *) arg0) {
8126 			/* We set systemShutdown = true during shutdown
8127 			 *  to prevent sleep at unexpected times while loginwindow is trying
8128 			 *  to shutdown apps and while the OS is trying to transition to
8129 			 *  complete power of.
8130 			 *
8131 			 *  Set to true during shutdown, as soon as loginwindow shows
8132 			 *  the "shutdown countdown dialog", through individual app
8133 			 *  termination, and through black screen kernel shutdown.
8134 			 */
8135 			systemShutdown = true;
8136 		} else {
8137 			/*
8138 			 *  A shutdown was initiated, but then the shutdown
8139 			 *  was cancelled, clearing systemShutdown to false here.
8140 			 */
8141 			systemShutdown = false;
8142 		}
8143 		break;
8144 
8145 	case kPowerEventUserDisabledSleep:
8146 		DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8147 		userDisabledAllSleep = (kOSBooleanTrue == (OSBoolean *) arg0);
8148 		break;
8149 
8150 	case kPowerEventRegisterSystemCapabilityClient:
8151 		DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8152 
8153 		// reset() handles the arg0 == nullptr case for us
8154 		systemCapabilityNotifier.reset((IONotifier *) arg0, OSRetain);
8155 		/* intentional fall-through */
8156 		[[clang::fallthrough]];
8157 
8158 	case kPowerEventRegisterKernelCapabilityClient:
8159 		DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8160 		if (!_joinedCapabilityClients) {
8161 			_joinedCapabilityClients = OSSet::withCapacity(8);
8162 		}
8163 		if (arg0) {
8164 			OSSharedPtr<IONotifier> notify((IONotifier *) arg0, OSNoRetain);
8165 			if (_joinedCapabilityClients) {
8166 				_joinedCapabilityClients->setObject(notify.get());
8167 				synchronizePowerTree( kIOPMSyncNoChildNotify );
8168 			}
8169 		}
8170 		break;
8171 
8172 	case kPowerEventPolicyStimulus:
8173 		DMSG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8174 		if (arg0) {
8175 			int stimulus = (int)(uintptr_t) arg0;
8176 			evaluatePolicy(stimulus, (uint32_t) arg1);
8177 		}
8178 		break;
8179 
8180 	case kPowerEventAssertionCreate:
8181 		DMSG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8182 		if (pmAssertions) {
8183 			pmAssertions->handleCreateAssertion((OSValueObject<PMAssertStruct> *)arg0);
8184 		}
8185 		break;
8186 
8187 
8188 	case kPowerEventAssertionRelease:
8189 		DMSG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8190 		if (pmAssertions) {
8191 			pmAssertions->handleReleaseAssertion(arg1);
8192 		}
8193 		break;
8194 
8195 	case kPowerEventAssertionSetLevel:
8196 		DMSG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8197 		if (pmAssertions) {
8198 			pmAssertions->handleSetAssertionLevel(arg1, (IOPMDriverAssertionLevel)(uintptr_t)arg0);
8199 		}
8200 		break;
8201 
8202 	case kPowerEventQueueSleepWakeUUID:
8203 		DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8204 		handleQueueSleepWakeUUID((OSObject *)arg0);
8205 		break;
8206 	case kPowerEventPublishSleepWakeUUID:
8207 		DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8208 		handlePublishSleepWakeUUID((bool)arg0);
8209 		break;
8210 
8211 	case kPowerEventSetDisplayPowerOn:
8212 		DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8213 		if (arg1 != 0) {
8214 			displayPowerOnRequested = true;
8215 		} else {
8216 			displayPowerOnRequested = false;
8217 		}
8218 		handleSetDisplayPowerOn(displayPowerOnRequested);
8219 		break;
8220 
8221 	case kPowerEventPublishWakeType:
8222 		DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8223 
8224 		// Don't replace wake type property if already set
8225 		if ((arg0 == gIOPMWakeTypeUserKey) ||
8226 		    !propertyExists(kIOPMRootDomainWakeTypeKey)) {
8227 			const char * wakeType = NULL;
8228 
8229 			if (arg0 == gIOPMWakeTypeUserKey) {
8230 				requestUserActive(this, "WakeTypeUser");
8231 				wakeType = kIOPMRootDomainWakeTypeUser;
8232 			} else if (arg0 == gIOPMSettingDebugWakeRelativeKey) {
8233 				if (!(gDarkWakeFlags & kDarkWakeFlagAlarmIsDark)) {
8234 					requestUserActive(this, "WakeTypeAlarm");
8235 				}
8236 				wakeType = kIOPMRootDomainWakeTypeAlarm;
8237 			} else if (arg0 == gIOPMSettingSleepServiceWakeCalendarKey) {
8238 				darkWakeSleepService = true;
8239 				wakeType = kIOPMRootDomainWakeTypeSleepService;
8240 			} else if (arg0 == gIOPMSettingMaintenanceWakeCalendarKey) {
8241 				wakeType = kIOPMRootDomainWakeTypeMaintenance;
8242 			}
8243 
8244 			if (wakeType) {
8245 				setProperty(kIOPMRootDomainWakeTypeKey, wakeType);
8246 			}
8247 		}
8248 		break;
8249 
8250 	case kPowerEventAOTEvaluate:
8251 		DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8252 		if (_aotReadyToFullWake) {
8253 			aotEvaluate(NULL);
8254 		}
8255 		break;
8256 	}
8257 }
8258 
8259 //******************************************************************************
8260 // systemPowerEventOccurred
8261 //
8262 // The power controller is notifying us of a hardware-related power management
8263 // event that we must handle.
8264 //
8265 // systemPowerEventOccurred covers the same functionality that
8266 // receivePowerNotification does; it simply provides a richer API for conveying
8267 // more information.
8268 //******************************************************************************
8269 
8270 IOReturn
8271 IOPMrootDomain::systemPowerEventOccurred(
8272 	const OSSymbol *event,
8273 	uint32_t intValue)
8274 {
8275 	IOReturn        attempt = kIOReturnSuccess;
8276 	OSSharedPtr<OSNumber>        newNumber;
8277 
8278 	if (!event) {
8279 		return kIOReturnBadArgument;
8280 	}
8281 
8282 	newNumber = OSNumber::withNumber(intValue, 8 * sizeof(intValue));
8283 	if (!newNumber) {
8284 		return kIOReturnInternalError;
8285 	}
8286 
8287 	attempt = systemPowerEventOccurred(event, static_cast<OSObject *>(newNumber.get()));
8288 
8289 	return attempt;
8290 }
8291 
8292 void
8293 IOPMrootDomain::setThermalState(OSObject *value)
8294 {
8295 	OSNumber * num;
8296 
8297 	if (gIOPMWorkLoop->inGate() == false) {
8298 		gIOPMWorkLoop->runAction(
8299 			OSMemberFunctionCast(IOWorkLoop::Action, this, &IOPMrootDomain::setThermalState),
8300 			(OSObject *)this,
8301 			(void *)value);
8302 
8303 		return;
8304 	}
8305 	if (value && (num = OSDynamicCast(OSNumber, value))) {
8306 		thermalWarningState = ((num->unsigned32BitValue() == kIOPMThermalLevelWarning) ||
8307 		    (num->unsigned32BitValue() == kIOPMThermalLevelTrap)) ? 1 : 0;
8308 	}
8309 }
8310 
8311 IOReturn
8312 IOPMrootDomain::systemPowerEventOccurred(
8313 	const OSSymbol *event,
8314 	OSObject *value)
8315 {
8316 	OSSharedPtr<OSDictionary> thermalsDict;
8317 	bool shouldUpdate = true;
8318 
8319 	if (!event || !value) {
8320 		return kIOReturnBadArgument;
8321 	}
8322 
8323 	// LOCK
8324 	// We reuse featuresDict Lock because it already exists and guards
8325 	// the very infrequently used publish/remove feature mechanism; so there's zero rsk
8326 	// of stepping on that lock.
8327 	if (featuresDictLock) {
8328 		IOLockLock(featuresDictLock);
8329 	}
8330 
8331 	OSSharedPtr<OSObject> origThermalsProp = copyProperty(kIOPMRootDomainPowerStatusKey);
8332 	OSDictionary * origThermalsDict = OSDynamicCast(OSDictionary, origThermalsProp.get());
8333 
8334 	if (origThermalsDict) {
8335 		thermalsDict = OSDictionary::withDictionary(origThermalsDict);
8336 	} else {
8337 		thermalsDict = OSDictionary::withCapacity(1);
8338 	}
8339 
8340 	if (!thermalsDict) {
8341 		shouldUpdate = false;
8342 		goto exit;
8343 	}
8344 
8345 	thermalsDict->setObject(event, value);
8346 
8347 	setProperty(kIOPMRootDomainPowerStatusKey, thermalsDict.get());
8348 
8349 exit:
8350 	// UNLOCK
8351 	if (featuresDictLock) {
8352 		IOLockUnlock(featuresDictLock);
8353 	}
8354 
8355 	if (shouldUpdate) {
8356 		if (event &&
8357 		    event->isEqualTo(kIOPMThermalLevelWarningKey)) {
8358 			setThermalState(value);
8359 		}
8360 		messageClients(kIOPMMessageSystemPowerEventOccurred, (void *)NULL);
8361 	}
8362 
8363 	return kIOReturnSuccess;
8364 }
8365 
8366 //******************************************************************************
8367 // receivePowerNotification
8368 //
8369 // The power controller is notifying us of a hardware-related power management
8370 // event that we must handle. This may be a result of an 'environment' interrupt
8371 // from the power mgt micro.
8372 //******************************************************************************
8373 
8374 IOReturn
8375 IOPMrootDomain::receivePowerNotification( UInt32 msg )
8376 {
8377 	if (msg & kIOPMPowerButton) {
8378 		uint32_t currentPhase = pmTracer->getTracePhase();
8379 		if (currentPhase != kIOPMTracePointSystemUp && currentPhase > kIOPMTracePointSystemSleep) {
8380 			DEBUG_LOG("power button pressed during wake. phase = %u\n", currentPhase);
8381 			swd_flags |= SWD_PWR_BTN_STACKSHOT;
8382 			thread_call_enter(powerButtonDown);
8383 		} else {
8384 			DEBUG_LOG("power button pressed when system is up\n");
8385 		}
8386 	} else if (msg & kIOPMPowerButtonUp) {
8387 		if (swd_flags & SWD_PWR_BTN_STACKSHOT) {
8388 			swd_flags &= ~SWD_PWR_BTN_STACKSHOT;
8389 			thread_call_enter(powerButtonUp);
8390 		}
8391 	} else {
8392 		pmPowerStateQueue->submitPowerEvent(
8393 			kPowerEventReceivedPowerNotification, (void *)(uintptr_t) msg );
8394 	}
8395 	return kIOReturnSuccess;
8396 }
8397 
8398 void
8399 IOPMrootDomain::handlePowerNotification( UInt32 msg )
8400 {
8401 	bool        eval_clamshell = false;
8402 	bool        eval_clamshell_alarm = false;
8403 
8404 	ASSERT_GATED();
8405 
8406 	/*
8407 	 * Local (IOPMrootDomain only) eval clamshell command
8408 	 */
8409 	if (msg & kLocalEvalClamshellCommand) {
8410 		if ((gClamshellFlags & kClamshell_WAR_47715679) && isRTCAlarmWake) {
8411 			eval_clamshell_alarm = true;
8412 
8413 			// reset isRTCAlarmWake. This evaluation should happen only once
8414 			// on RTC/Alarm wake. Any clamshell events after wake should follow
8415 			// the regular evaluation
8416 			isRTCAlarmWake = false;
8417 		} else {
8418 			eval_clamshell = true;
8419 		}
8420 	}
8421 
8422 	/*
8423 	 * Overtemp
8424 	 */
8425 	if (msg & kIOPMOverTemp) {
8426 		DLOG("Thermal overtemp message received!\n");
8427 		thermalEmergencyState = true;
8428 		privateSleepSystem(kIOPMSleepReasonThermalEmergency);
8429 	}
8430 
8431 	/*
8432 	 * Forward DW thermal notification to client, if system is not going to sleep
8433 	 */
8434 	if ((msg & kIOPMDWOverTemp) && (_systemTransitionType != kSystemTransitionSleep)) {
8435 		DLOG("DarkWake thermal limits message received!\n");
8436 		messageClients(kIOPMMessageDarkWakeThermalEmergency);
8437 	}
8438 
8439 	/*
8440 	 * Sleep Now!
8441 	 */
8442 	if (msg & kIOPMSleepNow) {
8443 		privateSleepSystem(kIOPMSleepReasonSoftware);
8444 	}
8445 
8446 	/*
8447 	 * Power Emergency
8448 	 */
8449 	if (msg & kIOPMPowerEmergency) {
8450 		DLOG("Received kIOPMPowerEmergency");
8451 #if HIBERNATION && defined(__arm64__)
8452 		if (!ml_is_secure_hib_supported()) {
8453 			// Wait for the next low battery notification if the system state is
8454 			// in transition.
8455 			if ((_systemTransitionType == kSystemTransitionNone) &&
8456 			    CAP_CURRENT(kIOPMSystemCapabilityCPU) &&
8457 			    !systemBooting && !systemShutdown && !gWillShutdown) {
8458 				// Setting lowBatteryCondition will prevent system sleep
8459 				lowBatteryCondition = true;
8460 
8461 				// Notify userspace to initiate system shutdown
8462 				messageClients(kIOPMMessageRequestSystemShutdown);
8463 			}
8464 		} else {
8465 			lowBatteryCondition = true;
8466 			privateSleepSystem(kIOPMSleepReasonLowPower);
8467 		}
8468 #else  /* HIBERNATION && defined(__arm64__) */
8469 		lowBatteryCondition = true;
8470 		privateSleepSystem(kIOPMSleepReasonLowPower);
8471 #endif /* HIBERNATION && defined(__arm64__) */
8472 	}
8473 
8474 	/*
8475 	 * Clamshell OPEN
8476 	 */
8477 	if (msg & kIOPMClamshellOpened) {
8478 		DLOG("Clamshell opened\n");
8479 		// Received clamshel open message from clamshell controlling driver
8480 		// Update our internal state and tell general interest clients
8481 		clamshellClosed = false;
8482 		clamshellExists = true;
8483 
8484 		// Don't issue a hid tickle when lid is open and polled on wake
8485 		if (msg & kIOPMSetValue) {
8486 			setProperty(kIOPMRootDomainWakeTypeKey, "Lid Open");
8487 			reportUserInput();
8488 		}
8489 
8490 		// Tell PMCPU
8491 		informCPUStateChange(kInformLid, 0);
8492 
8493 		// Tell general interest clients
8494 		sendClientClamshellNotification();
8495 
8496 		bool aborting =  ((lastSleepReason == kIOPMSleepReasonClamshell)
8497 		    || (lastSleepReason == kIOPMSleepReasonIdle)
8498 		    || (lastSleepReason == kIOPMSleepReasonMaintenance));
8499 		if (aborting) {
8500 			userActivityCount++;
8501 		}
8502 		DLOG("clamshell tickled %d lastSleepReason %d\n", userActivityCount, lastSleepReason);
8503 	}
8504 
8505 	/*
8506 	 * Clamshell CLOSED
8507 	 * Send the clamshell interest notification since the lid is closing.
8508 	 */
8509 	if (msg & kIOPMClamshellClosed) {
8510 		if ((clamshellIgnoreClose || (gClamshellFlags & kClamshell_WAR_38378787)) &&
8511 		    clamshellClosed && clamshellExists) {
8512 			DLOG("Ignoring redundant Clamshell close event\n");
8513 		} else {
8514 			DLOG("Clamshell closed\n");
8515 			// Received clamshel open message from clamshell controlling driver
8516 			// Update our internal state and tell general interest clients
8517 			clamshellClosed = true;
8518 			clamshellExists = true;
8519 
8520 			// Ignore all following clamshell close events until the clamshell
8521 			// is opened or the system sleeps. When a clamshell close triggers
8522 			// a system wake, the lid driver may send us two clamshell close
8523 			// events, one for the clamshell close event itself, and a second
8524 			// close event when the driver polls the lid state on wake.
8525 			clamshellIgnoreClose = true;
8526 
8527 			// Tell PMCPU
8528 			informCPUStateChange(kInformLid, 1);
8529 
8530 			// Tell general interest clients
8531 			sendClientClamshellNotification();
8532 
8533 			// And set eval_clamshell = so we can attempt
8534 			eval_clamshell = true;
8535 		}
8536 	}
8537 
8538 	/*
8539 	 * Set Desktop mode (sent from graphics)
8540 	 *
8541 	 *  -> reevaluate lid state
8542 	 */
8543 	if (msg & kIOPMSetDesktopMode) {
8544 		desktopMode = (0 != (msg & kIOPMSetValue));
8545 		msg &= ~(kIOPMSetDesktopMode | kIOPMSetValue);
8546 		DLOG("Desktop mode %d\n", desktopMode);
8547 
8548 		sendClientClamshellNotification();
8549 
8550 		// Re-evaluate the lid state
8551 		eval_clamshell = true;
8552 	}
8553 
8554 	/*
8555 	 * AC Adaptor connected
8556 	 *
8557 	 *  -> reevaluate lid state
8558 	 */
8559 	if (msg & kIOPMSetACAdaptorConnected) {
8560 		acAdaptorConnected = (0 != (msg & kIOPMSetValue));
8561 		msg &= ~(kIOPMSetACAdaptorConnected | kIOPMSetValue);
8562 
8563 		// Tell CPU PM
8564 		informCPUStateChange(kInformAC, !acAdaptorConnected);
8565 
8566 		// Tell BSD if AC is connected
8567 		//      0 == external power source; 1 == on battery
8568 		post_sys_powersource(acAdaptorConnected ? 0:1);
8569 
8570 		sendClientClamshellNotification();
8571 
8572 		IOUserServer::powerSourceChanged(acAdaptorConnected);
8573 
8574 		// Re-evaluate the lid state
8575 		eval_clamshell = true;
8576 
8577 		// Lack of AC may have latched a display wrangler tickle.
8578 		// This mirrors the hardware's USB wake event latch, where a latched
8579 		// USB wake event followed by an AC attach will trigger a full wake.
8580 		latchDisplayWranglerTickle( false );
8581 
8582 #if HIBERNATION
8583 		// AC presence will reset the standy timer delay adjustment.
8584 		_standbyTimerResetSeconds = 0;
8585 #endif
8586 		if (!userIsActive) {
8587 			// Reset userActivityTime when power supply is changed(rdr 13789330)
8588 			clock_get_uptime(&userActivityTime);
8589 		}
8590 	}
8591 
8592 	/*
8593 	 * Enable Clamshell (external display disappear)
8594 	 *
8595 	 *  -> reevaluate lid state
8596 	 */
8597 	if (msg & kIOPMEnableClamshell) {
8598 		DLOG("Clamshell enabled\n");
8599 
8600 		// Re-evaluate the lid state
8601 		// System should sleep on external display disappearance
8602 		// in lid closed operation.
8603 		if (true == clamshellDisabled) {
8604 			eval_clamshell = true;
8605 
8606 #if DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY
8607 			// Also clear kClamshellSleepDisableInternal when graphics enables
8608 			// the clamshell during a full wake. When graphics is behaving as
8609 			// expected, this will allow clamshell close to be honored earlier
8610 			// rather than waiting for the delayed evaluation.
8611 			if ((clamshellSleepDisableMask & kClamshellSleepDisableInternal) &&
8612 			    (CAP_PENDING(kIOPMSystemCapabilityGraphics) ||
8613 			    CAP_CURRENT(kIOPMSystemCapabilityGraphics))) {
8614 				setClamShellSleepDisable(false, kClamshellSleepDisableInternal);
8615 
8616 				// Cancel the TC to avoid an extra kLocalEvalClamshellCommand
8617 				// when timer expires which is harmless but useless.
8618 				thread_call_cancel(fullWakeThreadCall);
8619 			}
8620 #endif
8621 		}
8622 
8623 		clamshellDisabled = false;
8624 		sendClientClamshellNotification();
8625 	}
8626 
8627 	/*
8628 	 * Disable Clamshell (external display appeared)
8629 	 * We don't bother re-evaluating clamshell state. If the system is awake,
8630 	 * the lid is probably open.
8631 	 */
8632 	if (msg & kIOPMDisableClamshell) {
8633 		DLOG("Clamshell disabled\n");
8634 		clamshellDisabled = true;
8635 		sendClientClamshellNotification();
8636 	}
8637 
8638 	/*
8639 	 * Evaluate clamshell and SLEEP if appropriate
8640 	 */
8641 	if (eval_clamshell_alarm && clamshellClosed) {
8642 		if (shouldSleepOnRTCAlarmWake()) {
8643 			privateSleepSystem(kIOPMSleepReasonClamshell);
8644 		}
8645 	} else if (eval_clamshell && clamshellClosed) {
8646 		if (shouldSleepOnClamshellClosed()) {
8647 			privateSleepSystem(kIOPMSleepReasonClamshell);
8648 		} else {
8649 			evaluatePolicy( kStimulusDarkWakeEvaluate );
8650 		}
8651 	}
8652 
8653 	if (msg & kIOPMProModeEngaged) {
8654 		int newState = 1;
8655 		DLOG("ProModeEngaged\n");
8656 		messageClient(kIOPMMessageProModeStateChange, systemCapabilityNotifier.get(), &newState, sizeof(newState));
8657 	}
8658 
8659 	if (msg & kIOPMProModeDisengaged) {
8660 		int newState = 0;
8661 		DLOG("ProModeDisengaged\n");
8662 		messageClient(kIOPMMessageProModeStateChange, systemCapabilityNotifier.get(), &newState, sizeof(newState));
8663 	}
8664 }
8665 
8666 //******************************************************************************
8667 // evaluatePolicy
8668 //
8669 // Evaluate root-domain policy in response to external changes.
8670 //******************************************************************************
8671 
8672 void
8673 IOPMrootDomain::evaluatePolicy( int stimulus, uint32_t arg )
8674 {
8675 	union {
8676 		struct {
8677 			int idleSleepEnabled    : 1;
8678 			int idleSleepDisabled   : 1;
8679 			int displaySleep        : 1;
8680 			int sleepDelayChanged   : 1;
8681 			int evaluateDarkWake    : 1;
8682 			int adjustPowerState    : 1;
8683 			int userBecameInactive  : 1;
8684 			int displaySleepEntry   : 1;
8685 		} bit;
8686 		uint32_t u32;
8687 	} flags;
8688 
8689 
8690 	ASSERT_GATED();
8691 	flags.u32 = 0;
8692 
8693 	switch (stimulus) {
8694 	case kStimulusDisplayWranglerSleep:
8695 		DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8696 		if (!wranglerPowerOff) {
8697 			// wrangler is in sleep state or lower
8698 			flags.bit.displaySleep = true;
8699 		}
8700 		if (!wranglerAsleep) {
8701 			// transition from wrangler wake to wrangler sleep
8702 			flags.bit.displaySleepEntry = true;
8703 			wranglerAsleep = true;
8704 		}
8705 		break;
8706 
8707 	case kStimulusDisplayWranglerWake:
8708 		DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8709 		displayIdleForDemandSleep = false;
8710 		wranglerPowerOff = false;
8711 		wranglerAsleep = false;
8712 		break;
8713 
8714 	case kStimulusEnterUserActiveState:
8715 		DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8716 		if (_preventUserActive) {
8717 			DLOG("user active dropped\n");
8718 			break;
8719 		}
8720 		if (!userIsActive) {
8721 			userIsActive = true;
8722 			userWasActive = true;
8723 			clock_get_uptime(&gUserActiveAbsTime);
8724 
8725 			// Stay awake after dropping demand for display power on
8726 			if (kFullWakeReasonDisplayOn == fullWakeReason) {
8727 				fullWakeReason = fFullWakeReasonDisplayOnAndLocalUser;
8728 				DLOG("User activity while in notification wake\n");
8729 				changePowerStateWithOverrideTo( getRUN_STATE(), 0);
8730 			}
8731 
8732 			kdebugTrace(kPMLogUserActiveState, 0, 1, 0);
8733 			setProperty(gIOPMUserIsActiveKey.get(), kOSBooleanTrue);
8734 			messageClients(kIOPMMessageUserIsActiveChanged);
8735 		}
8736 		flags.bit.idleSleepDisabled = true;
8737 		break;
8738 
8739 	case kStimulusLeaveUserActiveState:
8740 		DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8741 		if (userIsActive) {
8742 			clock_get_uptime(&gUserInactiveAbsTime);
8743 			userIsActive = false;
8744 			clock_get_uptime(&userBecameInactiveTime);
8745 			flags.bit.userBecameInactive = true;
8746 
8747 			kdebugTrace(kPMLogUserActiveState, 0, 0, 0);
8748 			setProperty(gIOPMUserIsActiveKey.get(), kOSBooleanFalse);
8749 			messageClients(kIOPMMessageUserIsActiveChanged);
8750 		}
8751 		break;
8752 
8753 	case kStimulusAggressivenessChanged:
8754 	{
8755 		DMSG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8756 		unsigned long   aggressiveValue;
8757 		uint32_t        minutesToIdleSleep  = 0;
8758 		uint32_t        minutesToDisplayDim = 0;
8759 		uint32_t        minutesDelta        = 0;
8760 
8761 		// Fetch latest display and system sleep slider values.
8762 		aggressiveValue = 0;
8763 		getAggressiveness(kPMMinutesToSleep, &aggressiveValue);
8764 		minutesToIdleSleep = (uint32_t) aggressiveValue;
8765 
8766 		aggressiveValue = 0;
8767 		getAggressiveness(kPMMinutesToDim, &aggressiveValue);
8768 		minutesToDisplayDim = (uint32_t) aggressiveValue;
8769 		DLOG("aggressiveness changed: system %u->%u, display %u\n",
8770 		    sleepSlider, minutesToIdleSleep, minutesToDisplayDim);
8771 
8772 		DLOG("idle time -> %d ms (ena %d)\n",
8773 		    idleMilliSeconds, (minutesToIdleSleep != 0));
8774 
8775 		// How long to wait before sleeping the system once
8776 		// the displays turns off is indicated by 'extraSleepDelay'.
8777 
8778 		if (minutesToIdleSleep > minutesToDisplayDim) {
8779 			minutesDelta = minutesToIdleSleep - minutesToDisplayDim;
8780 		} else if (minutesToIdleSleep == minutesToDisplayDim) {
8781 			minutesDelta = 1;
8782 		}
8783 
8784 		if ((!idleSleepEnabled) && (minutesToIdleSleep != 0)) {
8785 			idleSleepEnabled = flags.bit.idleSleepEnabled = true;
8786 		}
8787 
8788 		if ((idleSleepEnabled) && (minutesToIdleSleep == 0)) {
8789 			flags.bit.idleSleepDisabled = true;
8790 			idleSleepEnabled = false;
8791 		}
8792 #if !defined(XNU_TARGET_OS_OSX)
8793 		if (0x7fffffff == minutesToIdleSleep) {
8794 			minutesToIdleSleep = idleMilliSeconds / 1000;
8795 		}
8796 #endif /* !defined(XNU_TARGET_OS_OSX) */
8797 
8798 		if (((minutesDelta != extraSleepDelay) ||
8799 		    (userActivityTime != userActivityTime_prev)) &&
8800 		    !flags.bit.idleSleepEnabled && !flags.bit.idleSleepDisabled) {
8801 			flags.bit.sleepDelayChanged = true;
8802 		}
8803 
8804 		if (systemDarkWake && !darkWakeToSleepASAP &&
8805 		    (flags.bit.idleSleepEnabled || flags.bit.idleSleepDisabled)) {
8806 			// Reconsider decision to remain in dark wake
8807 			flags.bit.evaluateDarkWake = true;
8808 		}
8809 
8810 		sleepSlider = minutesToIdleSleep;
8811 		extraSleepDelay = minutesDelta;
8812 		userActivityTime_prev = userActivityTime;
8813 	}   break;
8814 
8815 	case kStimulusDemandSystemSleep:
8816 		DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8817 		displayIdleForDemandSleep = true;
8818 		if (wrangler && wranglerIdleSettings) {
8819 			// Request wrangler idle only when demand sleep is triggered
8820 			// from full wake.
8821 			if (CAP_CURRENT(kIOPMSystemCapabilityGraphics)) {
8822 				wrangler->setProperties(wranglerIdleSettings.get());
8823 				DLOG("Requested wrangler idle\n");
8824 			}
8825 		}
8826 		// arg = sleepReason
8827 		changePowerStateWithOverrideTo( SLEEP_STATE, arg );
8828 		break;
8829 
8830 	case kStimulusAllowSystemSleepChanged:
8831 		DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8832 		flags.bit.adjustPowerState = true;
8833 		break;
8834 
8835 	case kStimulusDarkWakeActivityTickle:
8836 		DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8837 		// arg == true implies real and not self generated wrangler tickle.
8838 		// Update wake type on PM work loop instead of the tickle thread to
8839 		// eliminate the possibility of an early tickle clobbering the wake
8840 		// type set by the platform driver.
8841 		if (arg == true) {
8842 			setProperty(kIOPMRootDomainWakeTypeKey, kIOPMRootDomainWakeTypeHIDActivity);
8843 		}
8844 
8845 		if (!darkWakeExit) {
8846 			if (latchDisplayWranglerTickle(true)) {
8847 				DLOG("latched tickle\n");
8848 				break;
8849 			}
8850 
8851 			darkWakeExit = true;
8852 			DLOG("Requesting full wake due to dark wake activity tickle\n");
8853 			requestFullWake( kFullWakeReasonLocalUser );
8854 		}
8855 		break;
8856 
8857 	case kStimulusDarkWakeEntry:
8858 	case kStimulusDarkWakeReentry:
8859 		DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8860 		// Any system transitions since the last dark wake transition
8861 		// will invalid the stimulus.
8862 
8863 		if (arg == _systemStateGeneration) {
8864 			DLOG("dark wake entry\n");
8865 			systemDarkWake = true;
8866 
8867 			// Keep wranglerPowerOff an invariant when wrangler is absent
8868 			if (wrangler) {
8869 				wranglerPowerOff = true;
8870 			}
8871 
8872 			if (kStimulusDarkWakeEntry == stimulus) {
8873 				clock_get_uptime(&userBecameInactiveTime);
8874 				flags.bit.evaluateDarkWake = true;
8875 				if (activitySinceSleep()) {
8876 					DLOG("User activity recorded while going to darkwake\n");
8877 					reportUserInput();
8878 				}
8879 			}
8880 
8881 			// Always accelerate disk spindown while in dark wake,
8882 			// even if system does not support/allow sleep.
8883 
8884 			cancelIdleSleepTimer();
8885 			setQuickSpinDownTimeout();
8886 		}
8887 		break;
8888 
8889 	case kStimulusDarkWakeEvaluate:
8890 		DMSG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8891 		if (systemDarkWake) {
8892 			flags.bit.evaluateDarkWake = true;
8893 		}
8894 		break;
8895 
8896 	case kStimulusNoIdleSleepPreventers:
8897 		DMSG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8898 		flags.bit.adjustPowerState = true;
8899 		break;
8900 	} /* switch(stimulus) */
8901 
8902 	if (flags.bit.evaluateDarkWake && (kFullWakeReasonNone == fullWakeReason)) {
8903 		DLOG("DarkWake: sleepASAP %d, clamshell closed %d, disabled %d/%x, desktopMode %d, ac %d\n",
8904 		    darkWakeToSleepASAP, clamshellClosed, clamshellDisabled, clamshellSleepDisableMask, desktopMode, acAdaptorConnected);
8905 		if (darkWakeToSleepASAP ||
8906 		    (clamshellClosed && !(desktopMode && acAdaptorConnected))) {
8907 			uint32_t newSleepReason;
8908 
8909 			if (CAP_HIGHEST(kIOPMSystemCapabilityGraphics)) {
8910 				// System was previously in full wake. Sleep reason from
8911 				// full to dark already recorded in fullToDarkReason.
8912 
8913 				if (lowBatteryCondition) {
8914 					newSleepReason = kIOPMSleepReasonLowPower;
8915 				} else if (thermalEmergencyState) {
8916 					newSleepReason = kIOPMSleepReasonThermalEmergency;
8917 				} else {
8918 					newSleepReason = fullToDarkReason;
8919 				}
8920 			} else {
8921 				// In dark wake from system sleep.
8922 
8923 				if (darkWakeSleepService) {
8924 					newSleepReason = kIOPMSleepReasonSleepServiceExit;
8925 				} else {
8926 					newSleepReason = kIOPMSleepReasonMaintenance;
8927 				}
8928 			}
8929 
8930 			if (checkSystemCanSleep(newSleepReason)) {
8931 				privateSleepSystem(newSleepReason);
8932 			}
8933 		} else { // non-maintenance (network) dark wake
8934 			if (checkSystemCanSleep(kIOPMSleepReasonIdle)) {
8935 				// Release power clamp, and wait for children idle.
8936 				adjustPowerState(true);
8937 			} else {
8938 				changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonDarkWakeCannotSleep);
8939 			}
8940 		}
8941 	}
8942 
8943 	if (systemDarkWake) {
8944 		// The rest are irrelevant while system is in dark wake.
8945 		flags.u32 = 0;
8946 	}
8947 
8948 	if ((flags.bit.displaySleepEntry) &&
8949 	    (kFullWakeReasonDisplayOn == fullWakeReason)) {
8950 		// kIOPMSleepReasonNotificationWakeExit
8951 		DLOG("Display sleep while in notification wake\n");
8952 		changePowerStateWithOverrideTo(SLEEP_STATE, kIOPMSleepReasonNotificationWakeExit);
8953 	}
8954 
8955 	if (flags.bit.userBecameInactive || flags.bit.sleepDelayChanged) {
8956 		bool cancelQuickSpindown = false;
8957 
8958 		if (flags.bit.sleepDelayChanged) {
8959 			// Cancel existing idle sleep timer and quick disk spindown.
8960 			// New settings will be applied by the idleSleepEnabled flag
8961 			// handler below if idle sleep is enabled.
8962 
8963 			DLOG("extra sleep timer changed\n");
8964 			cancelIdleSleepTimer();
8965 			cancelQuickSpindown = true;
8966 		} else {
8967 			DLOG("user inactive\n");
8968 		}
8969 
8970 		if (!userIsActive && idleSleepEnabled) {
8971 			startIdleSleepTimer(getTimeToIdleSleep());
8972 		}
8973 
8974 		if (cancelQuickSpindown) {
8975 			restoreUserSpinDownTimeout();
8976 		}
8977 	}
8978 
8979 	if (flags.bit.idleSleepEnabled) {
8980 		DLOG("idle sleep timer enabled\n");
8981 		if (!wrangler) {
8982 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
8983 			startIdleSleepTimer(getTimeToIdleSleep());
8984 #else
8985 			changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonIdleSleepEnabled);
8986 			startIdleSleepTimer( idleMilliSeconds );
8987 #endif
8988 		} else {
8989 			// Start idle timer if prefs now allow system sleep
8990 			// and user is already inactive. Disk spindown is
8991 			// accelerated upon timer expiration.
8992 
8993 			if (!userIsActive) {
8994 				startIdleSleepTimer(getTimeToIdleSleep());
8995 			}
8996 		}
8997 	}
8998 
8999 	if (flags.bit.idleSleepDisabled) {
9000 		DLOG("idle sleep timer disabled\n");
9001 		cancelIdleSleepTimer();
9002 		restoreUserSpinDownTimeout();
9003 		adjustPowerState();
9004 	}
9005 
9006 	if (flags.bit.adjustPowerState) {
9007 		bool sleepASAP = false;
9008 
9009 		if (!systemBooting && (0 == idleSleepPreventersCount())) {
9010 			if (!wrangler) {
9011 				changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonEvaluatePolicy);
9012 				if (idleSleepEnabled) {
9013 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
9014 					if (!extraSleepDelay && !idleSleepTimerPending && !gNoIdleFlag) {
9015 						sleepASAP = true;
9016 					}
9017 #else
9018 					// stay awake for at least idleMilliSeconds
9019 					startIdleSleepTimer(idleMilliSeconds);
9020 #endif
9021 				}
9022 			} else if (!extraSleepDelay && !idleSleepTimerPending && !systemDarkWake && !gNoIdleFlag) {
9023 				sleepASAP = true;
9024 			}
9025 		}
9026 
9027 		adjustPowerState(sleepASAP);
9028 	}
9029 }
9030 
9031 //******************************************************************************
9032 
9033 unsigned int
9034 IOPMrootDomain::idleSleepPreventersCount()
9035 {
9036 	if (_aotMode) {
9037 		unsigned int count __block;
9038 		count = 0;
9039 		preventIdleSleepList->iterateObjects(^bool (OSObject * obj)
9040 		{
9041 			count += (NULL == obj->metaCast("AppleARMBacklight"));
9042 			return false;
9043 		});
9044 		return count;
9045 	}
9046 
9047 	return preventIdleSleepList->getCount();
9048 }
9049 
9050 
9051 //******************************************************************************
9052 // requestFullWake
9053 //
9054 // Request transition from dark wake to full wake
9055 //******************************************************************************
9056 
9057 void
9058 IOPMrootDomain::requestFullWake( FullWakeReason reason )
9059 {
9060 	uint32_t        options = 0;
9061 	IOService *     pciRoot = NULL;
9062 	bool            promotion = false;
9063 
9064 	// System must be in dark wake and a valid reason for entering full wake
9065 	if ((kFullWakeReasonNone == reason) ||
9066 	    (kFullWakeReasonNone != fullWakeReason) ||
9067 	    (CAP_CURRENT(kIOPMSystemCapabilityGraphics))) {
9068 		return;
9069 	}
9070 
9071 	// Will clear reason upon exit from full wake
9072 	fullWakeReason = reason;
9073 
9074 	_desiredCapability |= (kIOPMSystemCapabilityGraphics |
9075 	    kIOPMSystemCapabilityAudio);
9076 
9077 	if ((kSystemTransitionWake == _systemTransitionType) &&
9078 	    !(_pendingCapability & kIOPMSystemCapabilityGraphics) &&
9079 	    !darkWakePowerClamped) {
9080 		// Promote to full wake while waking up to dark wake due to tickle.
9081 		// PM will hold off notifying the graphics subsystem about system wake
9082 		// as late as possible, so if a HID tickle does arrive, graphics can
9083 		// power up from this same wake transition. Otherwise, the latency to
9084 		// power up graphics on the following transition can be huge on certain
9085 		// systems. However, once any power clamping has taken effect, it is
9086 		// too late to promote the current dark wake transition to a full wake.
9087 		_pendingCapability |= (kIOPMSystemCapabilityGraphics |
9088 		    kIOPMSystemCapabilityAudio);
9089 
9090 		// Tell the PCI parent of audio and graphics drivers to stop
9091 		// delaying the child notifications. Same for root domain.
9092 		pciRoot = pciHostBridgeDriver.get();
9093 		willEnterFullWake();
9094 		promotion = true;
9095 	}
9096 
9097 	// Unsafe to cancel once graphics was powered.
9098 	// If system woke from dark wake, the return to sleep can
9099 	// be cancelled. "awake -> dark -> sleep" transition
9100 	// can be cancelled also, during the "dark -> sleep" phase
9101 	// *prior* to driver power down.
9102 	if (!CAP_HIGHEST(kIOPMSystemCapabilityGraphics) ||
9103 	    _pendingCapability == 0) {
9104 		options |= kIOPMSyncCancelPowerDown;
9105 	}
9106 
9107 	synchronizePowerTree(options, pciRoot);
9108 
9109 	if (kFullWakeReasonLocalUser == fullWakeReason) {
9110 		// IOGraphics doesn't light the display even though graphics is
9111 		// enabled in kIOMessageSystemCapabilityChange message(radar 9502104)
9112 		// So, do an explicit activity tickle
9113 		if (wrangler) {
9114 			wrangler->activityTickle(0, 0);
9115 		}
9116 	}
9117 
9118 	// Log a timestamp for the initial full wake request.
9119 	// System may not always honor this full wake request.
9120 	if (!CAP_HIGHEST(kIOPMSystemCapabilityGraphics)) {
9121 		AbsoluteTime    now;
9122 		uint64_t        nsec;
9123 
9124 		clock_get_uptime(&now);
9125 		SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
9126 		absolutetime_to_nanoseconds(now, &nsec);
9127 		MSG("full wake %s (reason %u) %u ms\n",
9128 		    promotion ? "promotion" : "request",
9129 		    fullWakeReason, ((int)((nsec) / NSEC_PER_MSEC)));
9130 	}
9131 }
9132 
9133 //******************************************************************************
9134 // willEnterFullWake
9135 //
9136 // System will enter full wake from sleep, from dark wake, or from dark
9137 // wake promotion. This function aggregate things that are in common to
9138 // all three full wake transitions.
9139 //
9140 // Assumptions: fullWakeReason was updated
9141 //******************************************************************************
9142 
9143 void
9144 IOPMrootDomain::willEnterFullWake( void )
9145 {
9146 	hibernateRetry = false;
9147 	sleepToStandby = false;
9148 	standbyNixed   = false;
9149 	resetTimers    = false;
9150 	sleepTimerMaintenance = false;
9151 
9152 	assert(!CAP_CURRENT(kIOPMSystemCapabilityGraphics));
9153 
9154 	_systemMessageClientMask = kSystemMessageClientPowerd |
9155 	    kSystemMessageClientLegacyApp;
9156 
9157 	if ((_highestCapability & kIOPMSystemCapabilityGraphics) == 0) {
9158 		// First time to attain full wake capability since the last wake
9159 		_systemMessageClientMask |= kSystemMessageClientKernel;
9160 
9161 		// Set kIOPMUserTriggeredFullWakeKey before full wake for IOGraphics
9162 		setProperty(gIOPMUserTriggeredFullWakeKey.get(),
9163 		    (kFullWakeReasonLocalUser == fullWakeReason) ?
9164 		    kOSBooleanTrue : kOSBooleanFalse);
9165 	}
9166 #if HIBERNATION
9167 	IOHibernateSetWakeCapabilities(_pendingCapability);
9168 #endif
9169 
9170 	IOService::setAdvisoryTickleEnable( true );
9171 	tellClients(kIOMessageSystemWillPowerOn);
9172 	preventTransitionToUserActive(false);
9173 }
9174 
9175 //******************************************************************************
9176 // fullWakeDelayedWork
9177 //
9178 // System has already entered full wake. Invoked by a delayed thread call.
9179 //******************************************************************************
9180 
9181 void
9182 IOPMrootDomain::fullWakeDelayedWork( void )
9183 {
9184 #if DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY
9185 	if (!gIOPMWorkLoop->inGate()) {
9186 		gIOPMWorkLoop->runAction(
9187 			OSMemberFunctionCast(IOWorkLoop::Action, this,
9188 			&IOPMrootDomain::fullWakeDelayedWork), this);
9189 		return;
9190 	}
9191 
9192 	DLOG("fullWakeDelayedWork cap cur %x pend %x high %x, clamshell disable %x/%x\n",
9193 	    _currentCapability, _pendingCapability, _highestCapability,
9194 	    clamshellDisabled, clamshellSleepDisableMask);
9195 
9196 	if (clamshellExists &&
9197 	    CAP_CURRENT(kIOPMSystemCapabilityGraphics) &&
9198 	    !CAP_CHANGE(kIOPMSystemCapabilityGraphics)) {
9199 		if (clamshellSleepDisableMask & kClamshellSleepDisableInternal) {
9200 			setClamShellSleepDisable(false, kClamshellSleepDisableInternal);
9201 		} else {
9202 			// Not the initial full wake after waking from sleep.
9203 			// Evaluate the clamshell for rdar://problem/9157444.
9204 			receivePowerNotification(kLocalEvalClamshellCommand);
9205 		}
9206 	}
9207 #endif
9208 }
9209 
9210 //******************************************************************************
9211 // evaluateAssertions
9212 //
9213 //******************************************************************************
9214 
9215 // Bitmask of all kernel assertions that prevent system idle sleep.
9216 // kIOPMDriverAssertionReservedBit7 is reserved for IOMediaBSDClient.
9217 #define NO_IDLE_SLEEP_ASSERTIONS_MASK \
9218 	(kIOPMDriverAssertionReservedBit7 | \
9219 	 kIOPMDriverAssertionPreventSystemIdleSleepBit)
9220 
9221 void
9222 IOPMrootDomain::evaluateAssertions(IOPMDriverAssertionType newAssertions, IOPMDriverAssertionType oldAssertions)
9223 {
9224 	IOPMDriverAssertionType changedBits = newAssertions ^ oldAssertions;
9225 
9226 	messageClients(kIOPMMessageDriverAssertionsChanged);
9227 
9228 	if (changedBits & kIOPMDriverAssertionPreventDisplaySleepBit) {
9229 		if (wrangler) {
9230 			bool value = (newAssertions & kIOPMDriverAssertionPreventDisplaySleepBit) ? true : false;
9231 
9232 			DLOG("wrangler->setIgnoreIdleTimer\(%d)\n", value);
9233 			wrangler->setIgnoreIdleTimer( value );
9234 		}
9235 	}
9236 
9237 	if (changedBits & kIOPMDriverAssertionCPUBit) {
9238 		if (_aotNow) {
9239 			IOLog("CPU assertions %d\n", (0 != (kIOPMDriverAssertionCPUBit & newAssertions)));
9240 		}
9241 		evaluatePolicy(_aotNow ? kStimulusNoIdleSleepPreventers : kStimulusDarkWakeEvaluate);
9242 		if (!assertOnWakeSecs && gIOLastWakeAbsTime) {
9243 			AbsoluteTime    now;
9244 			clock_usec_t    microsecs;
9245 			clock_get_uptime(&now);
9246 			SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
9247 			absolutetime_to_microtime(now, &assertOnWakeSecs, &microsecs);
9248 			if (assertOnWakeReport) {
9249 				HISTREPORT_TALLYVALUE(assertOnWakeReport, (int64_t)assertOnWakeSecs);
9250 				DLOG("Updated assertOnWake %lu\n", (unsigned long)assertOnWakeSecs);
9251 			}
9252 		}
9253 	}
9254 
9255 	if (changedBits & NO_IDLE_SLEEP_ASSERTIONS_MASK) {
9256 		if ((newAssertions & NO_IDLE_SLEEP_ASSERTIONS_MASK) != 0) {
9257 			if ((oldAssertions & NO_IDLE_SLEEP_ASSERTIONS_MASK) == 0) {
9258 				DLOG("PreventIdleSleep driver assertion raised\n");
9259 				bool ok = updatePreventIdleSleepList(this, true);
9260 				if (ok && (changedBits & kIOPMDriverAssertionPreventSystemIdleSleepBit)) {
9261 					// Cancel idle sleep if there is one in progress
9262 					cancelIdlePowerDown(this);
9263 				}
9264 			}
9265 		} else {
9266 			DLOG("PreventIdleSleep driver assertion dropped\n");
9267 			updatePreventIdleSleepList(this, false);
9268 		}
9269 	}
9270 }
9271 
9272 // MARK: -
9273 // MARK: Statistics
9274 
9275 //******************************************************************************
9276 // pmStats
9277 //
9278 //******************************************************************************
9279 
9280 void
9281 IOPMrootDomain::pmStatsRecordEvent(
9282 	int                 eventIndex,
9283 	AbsoluteTime        timestamp)
9284 {
9285 	bool        starting = eventIndex & kIOPMStatsEventStartFlag ? true:false;
9286 	bool        stopping = eventIndex & kIOPMStatsEventStopFlag ? true:false;
9287 	uint64_t    delta;
9288 	uint64_t    nsec;
9289 	OSSharedPtr<OSData> publishPMStats;
9290 
9291 	eventIndex &= ~(kIOPMStatsEventStartFlag | kIOPMStatsEventStopFlag);
9292 
9293 	absolutetime_to_nanoseconds(timestamp, &nsec);
9294 
9295 	switch (eventIndex) {
9296 	case kIOPMStatsHibernateImageWrite:
9297 		if (starting) {
9298 			gPMStats.hibWrite.start = nsec;
9299 		} else if (stopping) {
9300 			gPMStats.hibWrite.stop = nsec;
9301 		}
9302 
9303 		if (stopping) {
9304 			delta = gPMStats.hibWrite.stop - gPMStats.hibWrite.start;
9305 			IOLog("PMStats: Hibernate write took %qd ms\n", delta / NSEC_PER_MSEC);
9306 		}
9307 		break;
9308 	case kIOPMStatsHibernateImageRead:
9309 		if (starting) {
9310 			gPMStats.hibRead.start = nsec;
9311 		} else if (stopping) {
9312 			gPMStats.hibRead.stop = nsec;
9313 		}
9314 
9315 		if (stopping) {
9316 			delta = gPMStats.hibRead.stop - gPMStats.hibRead.start;
9317 			IOLog("PMStats: Hibernate read took %qd ms\n", delta / NSEC_PER_MSEC);
9318 
9319 			publishPMStats = OSData::withValue(gPMStats);
9320 			setProperty(kIOPMSleepStatisticsKey, publishPMStats.get());
9321 			bzero(&gPMStats, sizeof(gPMStats));
9322 		}
9323 		break;
9324 	}
9325 }
9326 
9327 /*
9328  * Appends a record of the application response to
9329  * IOPMrootDomain::pmStatsAppResponses
9330  */
9331 void
9332 IOPMrootDomain::pmStatsRecordApplicationResponse(
9333 	const OSSymbol      *response,
9334 	const char          *name,
9335 	int                 messageType,
9336 	uint32_t            delay_ms,
9337 	uint64_t            id,
9338 	OSObject            *object,
9339 	IOPMPowerStateIndex powerState,
9340 	bool                async)
9341 {
9342 	OSSharedPtr<OSDictionary>    responseDescription;
9343 	OSSharedPtr<OSNumber>        delayNum;
9344 	OSSharedPtr<OSNumber>        powerCaps;
9345 	OSSharedPtr<OSNumber>        pidNum;
9346 	OSSharedPtr<OSNumber>        msgNum;
9347 	OSSharedPtr<const OSSymbol>  appname;
9348 	OSSharedPtr<const OSSymbol>  sleep;
9349 	OSSharedPtr<const OSSymbol>  wake;
9350 	IOPMServiceInterestNotifier *notify = NULL;
9351 
9352 	if (object && (notify = OSDynamicCast(IOPMServiceInterestNotifier, object))) {
9353 		if (response->isEqualTo(gIOPMStatsResponseTimedOut.get())) {
9354 			notify->ackTimeoutCnt++;
9355 		} else {
9356 			notify->ackTimeoutCnt = 0;
9357 		}
9358 	}
9359 
9360 	if (response->isEqualTo(gIOPMStatsResponsePrompt.get()) ||
9361 	    (_systemTransitionType == kSystemTransitionNone) || (_systemTransitionType == kSystemTransitionNewCapClient)) {
9362 		return;
9363 	}
9364 
9365 
9366 	if (response->isEqualTo(gIOPMStatsDriverPSChangeSlow.get())) {
9367 		kdebugTrace(kPMLogDrvPSChangeDelay, id, messageType, delay_ms);
9368 	} else if (notify) {
9369 		// User space app or kernel capability client
9370 		if (id) {
9371 			kdebugTrace(kPMLogAppResponseDelay, id, notify->msgType, delay_ms);
9372 		} else {
9373 			kdebugTrace(kPMLogDrvResponseDelay, notify->uuid0, messageType, delay_ms);
9374 		}
9375 		notify->msgType = 0;
9376 	}
9377 
9378 	responseDescription = OSDictionary::withCapacity(5);
9379 	if (responseDescription) {
9380 		if (response) {
9381 			responseDescription->setObject(_statsResponseTypeKey.get(), response);
9382 		}
9383 
9384 		msgNum = OSNumber::withNumber(messageType, 32);
9385 		if (msgNum) {
9386 			responseDescription->setObject(_statsMessageTypeKey.get(), msgNum.get());
9387 		}
9388 
9389 		if (!name && notify && notify->identifier) {
9390 			name = notify->identifier->getCStringNoCopy();
9391 		}
9392 
9393 		if (name && (strlen(name) > 0)) {
9394 			appname = OSSymbol::withCString(name);
9395 			if (appname) {
9396 				responseDescription->setObject(_statsNameKey.get(), appname.get());
9397 			}
9398 		}
9399 
9400 		if (!id && notify) {
9401 			id = notify->uuid0;
9402 		}
9403 		pidNum = OSNumber::withNumber(id, 64);
9404 		if (pidNum) {
9405 			responseDescription->setObject(_statsPIDKey.get(), pidNum.get());
9406 		}
9407 
9408 		delayNum = OSNumber::withNumber(delay_ms, 32);
9409 		if (delayNum) {
9410 			responseDescription->setObject(_statsTimeMSKey.get(), delayNum.get());
9411 		}
9412 
9413 		if (response->isEqualTo(gIOPMStatsDriverPSChangeSlow.get())) {
9414 			powerCaps = OSNumber::withNumber(powerState, 32);
9415 
9416 #if !defined(__i386__) && !defined(__x86_64__) && (DEVELOPMENT || DEBUG)
9417 			static const char * driverCallTypes[] = {
9418 				[kDriverCallInformPreChange]  = "powerStateWillChangeTo",
9419 				[kDriverCallInformPostChange] = "powerStateDidChangeTo",
9420 				[kDriverCallSetPowerState]    = "setPowerState"
9421 			};
9422 
9423 			if (messageType < (sizeof(driverCallTypes) / sizeof(driverCallTypes[0]))) {
9424 				DLOG("%s[0x%qx]::%s(%u) %stook %d ms\n",
9425 				    name, id, driverCallTypes[messageType], (uint32_t) powerState,
9426 				    async ? "async " : "", delay_ms);
9427 			}
9428 #endif
9429 		} else {
9430 			powerCaps = OSNumber::withNumber(_pendingCapability, 32);
9431 		}
9432 		if (powerCaps) {
9433 			responseDescription->setObject(_statsPowerCapsKey.get(), powerCaps.get());
9434 		}
9435 
9436 		sleep = OSSymbol::withCString("Sleep");
9437 		wake = OSSymbol::withCString("Wake");
9438 		if (_systemTransitionType == kSystemTransitionSleep) {
9439 			responseDescription->setObject(kIOPMStatsSystemTransitionKey, sleep.get());
9440 		} else if (_systemTransitionType == kSystemTransitionWake) {
9441 			responseDescription->setObject(kIOPMStatsSystemTransitionKey, wake.get());
9442 		} else if (_systemTransitionType == kSystemTransitionCapability) {
9443 			if (CAP_LOSS(kIOPMSystemCapabilityGraphics)) {
9444 				responseDescription->setObject(kIOPMStatsSystemTransitionKey, sleep.get());
9445 			} else if (CAP_GAIN(kIOPMSystemCapabilityGraphics)) {
9446 				responseDescription->setObject(kIOPMStatsSystemTransitionKey, wake.get());
9447 			}
9448 		}
9449 
9450 		IOLockLock(pmStatsLock);
9451 		if (pmStatsAppResponses && pmStatsAppResponses->getCount() < 50) {
9452 			pmStatsAppResponses->setObject(responseDescription.get());
9453 		}
9454 		IOLockUnlock(pmStatsLock);
9455 	}
9456 
9457 	return;
9458 }
9459 
9460 // MARK: -
9461 // MARK: PMTraceWorker
9462 
9463 //******************************************************************************
9464 // TracePoint support
9465 //
9466 //******************************************************************************
9467 
9468 #define kIOPMRegisterNVRAMTracePointHandlerKey  \
9469 	"IOPMRegisterNVRAMTracePointHandler"
9470 
9471 IOReturn
9472 IOPMrootDomain::callPlatformFunction(
9473 	const OSSymbol * functionName,
9474 	bool waitForFunction,
9475 	void * param1, void * param2,
9476 	void * param3, void * param4 )
9477 {
9478 	if (pmTracer && functionName &&
9479 	    functionName->isEqualTo(kIOPMRegisterNVRAMTracePointHandlerKey) &&
9480 	    !pmTracer->tracePointHandler && !pmTracer->tracePointTarget) {
9481 		uint32_t    tracePointPhases, tracePointPCI;
9482 		uint64_t    statusCode;
9483 
9484 		pmTracer->tracePointHandler = (IOPMTracePointHandler) param1;
9485 		pmTracer->tracePointTarget  = (void *) param2;
9486 		tracePointPCI               = (uint32_t)(uintptr_t) param3;
9487 		tracePointPhases            = (uint32_t)(uintptr_t) param4;
9488 		if ((tracePointPhases & 0xff) == kIOPMTracePointSystemSleep) {
9489 			OSSharedPtr<IORegistryEntry> node = IORegistryEntry::fromPath( "/chosen", gIODTPlane );
9490 			if (node) {
9491 				OSSharedPtr<OSObject> bootRomFailureProp;
9492 				bootRomFailureProp = node->copyProperty(kIOEFIBootRomFailureKey);
9493 				OSData *data = OSDynamicCast(OSData, bootRomFailureProp.get());
9494 				uint32_t bootFailureCode;
9495 				if (data && data->getLength() == sizeof(bootFailureCode)) {
9496 					// Failure code from EFI/BootRom is a four byte structure
9497 					memcpy(&bootFailureCode, data->getBytesNoCopy(), sizeof(bootFailureCode));
9498 					tracePointPCI = OSSwapBigToHostInt32(bootFailureCode);
9499 				}
9500 			}
9501 		}
9502 		statusCode = (((uint64_t)tracePointPCI) << 32) | tracePointPhases;
9503 		if ((tracePointPhases & 0xff) != kIOPMTracePointSystemUp) {
9504 			MSG("Sleep failure code 0x%08x 0x%08x\n",
9505 			    tracePointPCI, tracePointPhases);
9506 		}
9507 		setProperty(kIOPMSleepWakeFailureCodeKey, statusCode, 64);
9508 		pmTracer->tracePointHandler( pmTracer->tracePointTarget, 0, 0 );
9509 
9510 		return kIOReturnSuccess;
9511 	}
9512 #if HIBERNATION
9513 	else if (functionName &&
9514 	    functionName->isEqualTo(kIOPMInstallSystemSleepPolicyHandlerKey)) {
9515 		if (gSleepPolicyHandler) {
9516 			return kIOReturnExclusiveAccess;
9517 		}
9518 		if (!param1) {
9519 			return kIOReturnBadArgument;
9520 		}
9521 		gSleepPolicyHandler = (IOPMSystemSleepPolicyHandler) param1;
9522 		gSleepPolicyTarget  = (void *) param2;
9523 		setProperty("IOPMSystemSleepPolicyHandler", kOSBooleanTrue);
9524 		return kIOReturnSuccess;
9525 	}
9526 #endif
9527 
9528 	return super::callPlatformFunction(
9529 		functionName, waitForFunction, param1, param2, param3, param4);
9530 }
9531 
9532 void
9533 IOPMrootDomain::kdebugTrace(uint32_t event, uint64_t id,
9534     uintptr_t param1, uintptr_t param2, uintptr_t param3)
9535 {
9536 	uint32_t code   = IODBG_POWER(event);
9537 	uint64_t regId  = id;
9538 	if (regId == 0) {
9539 		regId  = getRegistryEntryID();
9540 	}
9541 	KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, code, (uintptr_t) regId, param1, param2, param3, 0);
9542 }
9543 
9544 void
9545 IOPMrootDomain::tracePoint( uint8_t point )
9546 {
9547 	if (systemBooting) {
9548 		return;
9549 	}
9550 
9551 	if (kIOPMTracePointWakeCapabilityClients == point) {
9552 		acceptSystemWakeEvents(kAcceptSystemWakeEvents_Disable);
9553 	}
9554 
9555 	kdebugTrace(kPMLogSleepWakeTracePoint, 0, point, 0);
9556 	pmTracer->tracePoint(point);
9557 }
9558 
9559 static void
9560 kext_log_putc(char c)
9561 {
9562 	if (gKextNameEnd || gKextNamePos >= (sizeof(gKextNameBuf) - 1)) {
9563 		return;
9564 	}
9565 	if (c == '(' || c == '[' || c == ' ') {
9566 		c = 0;
9567 		gKextNameEnd = true;
9568 	}
9569 
9570 	gKextNameBuf[gKextNamePos++] = c;
9571 }
9572 
9573 static int
9574 kext_log(const char *fmt, ...)
9575 {
9576 	va_list listp;
9577 
9578 	va_start(listp, fmt);
9579 	_doprnt(fmt, &listp, &kext_log_putc, 16);
9580 	va_end(listp);
9581 
9582 	return 0;
9583 }
9584 
9585 static OSPtr<const OSSymbol>
9586 copyKextIdentifierWithAddress(vm_address_t address)
9587 {
9588 	OSSharedPtr<const OSSymbol> identifer;
9589 
9590 	IOLockLock(gHaltLogLock);
9591 
9592 	gKextNameEnd = false;
9593 	gKextNamePos = 0;
9594 	gKextNameBuf[0] = 0;
9595 
9596 	OSKext::printKextsInBacktrace(&address, 1, kext_log, OSKext::kPrintKextsLock | OSKext::kPrintKextsTerse);
9597 	gKextNameBuf[sizeof(gKextNameBuf) - 1] = 0;
9598 	identifer = OSSymbol::withCString((gKextNameBuf[0] != 0) ? gKextNameBuf : kOSKextKernelIdentifier);
9599 
9600 	IOLockUnlock(gHaltLogLock);
9601 
9602 	return identifer;
9603 }
9604 
9605 // Caller serialized using PM workloop
9606 const char *
9607 IOPMrootDomain::getNotificationClientName(OSObject *object)
9608 {
9609 	IOPMServiceInterestNotifier *notifier = (typeof(notifier))object;
9610 	const char *clientName = "UNKNOWN";
9611 
9612 	if (!notifier->clientName) {
9613 		// Check for user client
9614 		if (systemCapabilityNotifier && (((IOPMServiceInterestNotifier *) systemCapabilityNotifier.get())->handler == notifier->handler)) {
9615 			OSNumber *clientID = NULL;
9616 			messageClient(kIOMessageCopyClientID, object, &clientID);
9617 			if (clientID) {
9618 				OSSharedPtr<OSString> string(IOCopyLogNameForPID(clientID->unsigned32BitValue()), OSNoRetain);
9619 				if (string) {
9620 					notifier->clientName = OSSymbol::withString(string.get());
9621 				}
9622 				clientID->release();
9623 			}
9624 		} else if (notifier->identifier) {
9625 			notifier->clientName.reset(notifier->identifier.get(), OSRetain);
9626 		}
9627 	}
9628 
9629 	if (notifier->clientName) {
9630 		clientName = notifier->clientName->getCStringNoCopy();
9631 	}
9632 
9633 	return clientName;
9634 }
9635 
9636 void
9637 IOPMrootDomain::traceNotification(OSObject *object, bool start, uint64_t timestamp, uint32_t msgIndex)
9638 {
9639 	IOPMServiceInterestNotifier *notifier;
9640 
9641 	if (systemBooting) {
9642 		return;
9643 	}
9644 	notifier = OSDynamicCast(IOPMServiceInterestNotifier, object);
9645 	if (!notifier) {
9646 		return;
9647 	}
9648 
9649 	if (start) {
9650 		pmTracer->traceDetail(notifier->uuid0 >> 32);
9651 		kdebugTrace(kPMLogSleepWakeMessage, pmTracer->getTracePhase(),
9652 		    (uintptr_t) notifier->msgType, (uintptr_t) notifier->uuid0, (uintptr_t) notifier->uuid1);
9653 
9654 		// Update notifier state used for response/ack logging
9655 		notifier->msgIndex = msgIndex;
9656 		notifier->msgAbsTime = timestamp;
9657 
9658 		if (msgIndex != UINT_MAX) {
9659 			DLOG("%s[%u] to %s\n", getIOMessageString(notifier->msgType), msgIndex, getNotificationClientName(notifier));
9660 		} else {
9661 			DLOG("%s to %s\n", getIOMessageString(notifier->msgType), getNotificationClientName(notifier));
9662 		}
9663 
9664 		assert(notifierObject == NULL);
9665 		notifierThread = current_thread();
9666 		notifierObject.reset(notifier, OSRetain);
9667 	} else {
9668 		uint64_t nsec;
9669 		uint32_t delayMS;
9670 
9671 		SUB_ABSOLUTETIME(&timestamp, &notifier->msgAbsTime);
9672 		absolutetime_to_nanoseconds(timestamp, &nsec);
9673 		delayMS = (uint32_t)(nsec / 1000000ULL);
9674 		if (delayMS > notifier->maxMsgDelayMS) {
9675 			notifier->maxMsgDelayMS = delayMS;
9676 		}
9677 
9678 		assert(notifierObject == notifier);
9679 		notifierObject.reset();
9680 		notifierThread = NULL;
9681 	}
9682 }
9683 
9684 void
9685 IOPMrootDomain::traceNotificationAck(OSObject *object, uint32_t delay_ms)
9686 {
9687 	if (systemBooting) {
9688 		return;
9689 	}
9690 	IOPMServiceInterestNotifier *notifier = OSDynamicCast(IOPMServiceInterestNotifier, object);
9691 	if (!notifier) {
9692 		return;
9693 	}
9694 
9695 	kdebugTrace(kPMLogDrvResponseDelay, notifier->uuid0,
9696 	    (uintptr_t) notifier->uuid1, (uintptr_t) 0, (uintptr_t) delay_ms);
9697 
9698 	DLOG("%s[%u] ack from %s took %d ms\n",
9699 	    getIOMessageString(notifier->msgType), notifier->msgIndex, getNotificationClientName(notifier), delay_ms);
9700 	if (delay_ms > notifier->maxAckDelayMS) {
9701 		notifier->maxAckDelayMS = delay_ms;
9702 	}
9703 }
9704 
9705 void
9706 IOPMrootDomain::traceNotificationResponse(OSObject *object, uint32_t delay_ms, uint32_t ack_time_us)
9707 {
9708 	if (systemBooting) {
9709 		return;
9710 	}
9711 	IOPMServiceInterestNotifier *notifier = OSDynamicCast(IOPMServiceInterestNotifier, object);
9712 	if (!notifier) {
9713 		return;
9714 	}
9715 
9716 	kdebugTrace(kPMLogDrvResponseDelay, notifier->uuid0,
9717 	    (uintptr_t) notifier->uuid1, (uintptr_t)(ack_time_us / 1000), (uintptr_t) delay_ms);
9718 
9719 	if (ack_time_us == 0) {
9720 		// Client work is done and ack will not be forthcoming
9721 		DLOG("%s[%u] response from %s took %d ms\n",
9722 		    getIOMessageString(notifier->msgType), notifier->msgIndex, getNotificationClientName(notifier), delay_ms);
9723 	} else {
9724 		// Client needs more time and it must ack within ack_time_us
9725 		DLOG("%s[%u] response from %s took %d ms (ack in %d us)\n",
9726 		    getIOMessageString(notifier->msgType), notifier->msgIndex, getNotificationClientName(notifier), delay_ms, ack_time_us);
9727 	}
9728 }
9729 
9730 void
9731 IOPMrootDomain::traceFilteredNotification(OSObject *object)
9732 {
9733 	if ((kIOLogDebugPower & gIOKitDebug) == 0) {
9734 		return;
9735 	}
9736 	if (systemBooting) {
9737 		return;
9738 	}
9739 	IOPMServiceInterestNotifier *notifier = OSDynamicCast(IOPMServiceInterestNotifier, object);
9740 	if (!notifier) {
9741 		return;
9742 	}
9743 
9744 	DLOG("%s to %s dropped\n", getIOMessageString(notifier->msgType), getNotificationClientName(notifier));
9745 }
9746 
9747 void
9748 IOPMrootDomain::traceDetail(uint32_t msgType, uint32_t msgIndex, uint32_t delay)
9749 {
9750 	if (!systemBooting) {
9751 		uint32_t detail = ((msgType & 0xffff) << 16) | (delay & 0xffff);
9752 		pmTracer->traceDetail( detail );
9753 		kdebugTrace(kPMLogSleepWakeTracePoint, pmTracer->getTracePhase(), msgType, delay);
9754 		DLOG("trace point 0x%02x msgType 0x%x detail 0x%08x\n", pmTracer->getTracePhase(), msgType, delay);
9755 	}
9756 }
9757 
9758 void
9759 IOPMrootDomain::configureReportGated(uint64_t channel_id, uint64_t action, void *result)
9760 {
9761 	size_t      reportSize;
9762 	void        **report = NULL;
9763 	uint32_t    bktCnt;
9764 	uint32_t    bktSize;
9765 	uint32_t    *clientCnt;
9766 
9767 	ASSERT_GATED();
9768 
9769 	report = NULL;
9770 	if (channel_id == kAssertDelayChID) {
9771 		report = &assertOnWakeReport;
9772 		bktCnt = kAssertDelayBcktCnt;
9773 		bktSize = kAssertDelayBcktSize;
9774 		clientCnt = &assertOnWakeClientCnt;
9775 	} else if (channel_id == kSleepDelaysChID) {
9776 		report = &sleepDelaysReport;
9777 		bktCnt = kSleepDelaysBcktCnt;
9778 		bktSize = kSleepDelaysBcktSize;
9779 		clientCnt = &sleepDelaysClientCnt;
9780 	} else {
9781 		assert(false);
9782 		return;
9783 	}
9784 
9785 	switch (action) {
9786 	case kIOReportEnable:
9787 
9788 		if (*report) {
9789 			(*clientCnt)++;
9790 			break;
9791 		}
9792 
9793 		reportSize = HISTREPORT_BUFSIZE(bktCnt);
9794 		*report = IOMallocZeroData(reportSize);
9795 		if (*report == NULL) {
9796 			break;
9797 		}
9798 		HISTREPORT_INIT((uint16_t)bktCnt, bktSize, *report, reportSize,
9799 		    getRegistryEntryID(), channel_id, kIOReportCategoryPower);
9800 
9801 		if (channel_id == kAssertDelayChID) {
9802 			assertOnWakeSecs = 0;
9803 		}
9804 
9805 		break;
9806 
9807 	case kIOReportDisable:
9808 		if (*clientCnt == 0) {
9809 			break;
9810 		}
9811 		if (*clientCnt == 1) {
9812 			IOFreeData(*report, HISTREPORT_BUFSIZE(bktCnt));
9813 			*report = NULL;
9814 		}
9815 		(*clientCnt)--;
9816 
9817 		if (channel_id == kAssertDelayChID) {
9818 			assertOnWakeSecs = -1; // Invalid value to prevent updates
9819 		}
9820 		break;
9821 
9822 	case kIOReportGetDimensions:
9823 		if (*report) {
9824 			HISTREPORT_UPDATERES(*report, kIOReportGetDimensions, result);
9825 		}
9826 		break;
9827 	}
9828 
9829 	return;
9830 }
9831 
9832 IOReturn
9833 IOPMrootDomain::configureReport(IOReportChannelList    *channelList,
9834     IOReportConfigureAction action,
9835     void                   *result,
9836     void                   *destination)
9837 {
9838 	unsigned cnt;
9839 	uint64_t configAction = (uint64_t)action;
9840 
9841 	for (cnt = 0; cnt < channelList->nchannels; cnt++) {
9842 		if ((channelList->channels[cnt].channel_id == kSleepCntChID) ||
9843 		    (channelList->channels[cnt].channel_id == kDarkWkCntChID) ||
9844 		    (channelList->channels[cnt].channel_id == kUserWkCntChID)) {
9845 			if (action != kIOReportGetDimensions) {
9846 				continue;
9847 			}
9848 			SIMPLEREPORT_UPDATERES(kIOReportGetDimensions, result);
9849 		} else if ((channelList->channels[cnt].channel_id == kAssertDelayChID) ||
9850 		    (channelList->channels[cnt].channel_id == kSleepDelaysChID)) {
9851 			gIOPMWorkLoop->runAction(
9852 				OSMemberFunctionCast(IOWorkLoop::Action, this, &IOPMrootDomain::configureReportGated),
9853 				(OSObject *)this, (void *)channelList->channels[cnt].channel_id,
9854 				(void *)configAction, (void *)result);
9855 		}
9856 	}
9857 
9858 	return super::configureReport(channelList, action, result, destination);
9859 }
9860 
9861 IOReturn
9862 IOPMrootDomain::updateReportGated(uint64_t ch_id, void *result, IOBufferMemoryDescriptor *dest)
9863 {
9864 	uint32_t    size2cpy;
9865 	void        *data2cpy;
9866 	void        **report;
9867 
9868 	ASSERT_GATED();
9869 
9870 	report = NULL;
9871 	if (ch_id == kAssertDelayChID) {
9872 		report = &assertOnWakeReport;
9873 	} else if (ch_id == kSleepDelaysChID) {
9874 		report = &sleepDelaysReport;
9875 	} else {
9876 		assert(false);
9877 		return kIOReturnBadArgument;
9878 	}
9879 
9880 	if (*report == NULL) {
9881 		return kIOReturnNotOpen;
9882 	}
9883 
9884 	HISTREPORT_UPDATEPREP(*report, data2cpy, size2cpy);
9885 	if (size2cpy > (dest->getCapacity() - dest->getLength())) {
9886 		return kIOReturnOverrun;
9887 	}
9888 
9889 	HISTREPORT_UPDATERES(*report, kIOReportCopyChannelData, result);
9890 	dest->appendBytes(data2cpy, size2cpy);
9891 
9892 	return kIOReturnSuccess;
9893 }
9894 
9895 IOReturn
9896 IOPMrootDomain::updateReport(IOReportChannelList      *channelList,
9897     IOReportUpdateAction      action,
9898     void                     *result,
9899     void                     *destination)
9900 {
9901 	uint32_t size2cpy;
9902 	void *data2cpy;
9903 	uint8_t buf[SIMPLEREPORT_BUFSIZE];
9904 	IOBufferMemoryDescriptor *dest = OSDynamicCast(IOBufferMemoryDescriptor, (OSObject *)destination);
9905 	unsigned cnt;
9906 	uint64_t ch_id;
9907 
9908 	if (action != kIOReportCopyChannelData) {
9909 		goto exit;
9910 	}
9911 
9912 	for (cnt = 0; cnt < channelList->nchannels; cnt++) {
9913 		ch_id = channelList->channels[cnt].channel_id;
9914 
9915 		if ((ch_id == kAssertDelayChID) || (ch_id == kSleepDelaysChID)) {
9916 			gIOPMWorkLoop->runAction(
9917 				OSMemberFunctionCast(IOWorkLoop::Action, this, &IOPMrootDomain::updateReportGated),
9918 				(OSObject *)this, (void *)ch_id,
9919 				(void *)result, (void *)dest);
9920 			continue;
9921 		} else if ((ch_id == kSleepCntChID) ||
9922 		    (ch_id == kDarkWkCntChID) || (ch_id == kUserWkCntChID)) {
9923 			SIMPLEREPORT_INIT(buf, sizeof(buf), getRegistryEntryID(), ch_id, kIOReportCategoryPower);
9924 		} else {
9925 			continue;
9926 		}
9927 
9928 		if (ch_id == kSleepCntChID) {
9929 			SIMPLEREPORT_SETVALUE(buf, sleepCnt);
9930 		} else if (ch_id == kDarkWkCntChID) {
9931 			SIMPLEREPORT_SETVALUE(buf, darkWakeCnt);
9932 		} else if (ch_id == kUserWkCntChID) {
9933 			SIMPLEREPORT_SETVALUE(buf, displayWakeCnt);
9934 		}
9935 
9936 		SIMPLEREPORT_UPDATEPREP(buf, data2cpy, size2cpy);
9937 		SIMPLEREPORT_UPDATERES(kIOReportCopyChannelData, result);
9938 		dest->appendBytes(data2cpy, size2cpy);
9939 	}
9940 
9941 exit:
9942 	return super::updateReport(channelList, action, result, destination);
9943 }
9944 
9945 
9946 //******************************************************************************
9947 // PMTraceWorker Class
9948 //
9949 //******************************************************************************
9950 
9951 #undef super
9952 #define super OSObject
9953 OSDefineMetaClassAndStructors(PMTraceWorker, OSObject)
9954 
9955 #define kPMBestGuessPCIDevicesCount     25
9956 #define kPMMaxRTCBitfieldSize           32
9957 
9958 OSPtr<PMTraceWorker>
9959 PMTraceWorker::tracer(IOPMrootDomain * owner)
9960 {
9961 	OSSharedPtr<PMTraceWorker> me = OSMakeShared<PMTraceWorker>();
9962 	if (!me || !me->init()) {
9963 		return NULL;
9964 	}
9965 
9966 	DLOG("PMTraceWorker %p\n", OBFUSCATE(me.get()));
9967 
9968 	// Note that we cannot instantiate the PCI device -> bit mappings here, since
9969 	// the IODeviceTree has not yet been created by IOPlatformExpert. We create
9970 	// this dictionary lazily.
9971 	me->owner = owner;
9972 	me->pciDeviceBitMappings = NULL;
9973 	me->pmTraceWorkerLock = IOLockAlloc();
9974 	me->tracePhase = kIOPMTracePointSystemUp;
9975 	me->traceData32 = 0;
9976 	me->loginWindowData = 0;
9977 	me->coreDisplayData = 0;
9978 	me->coreGraphicsData = 0;
9979 	return me;
9980 }
9981 
9982 void
9983 PMTraceWorker::RTC_TRACE(void)
9984 {
9985 	if (tracePointHandler && tracePointTarget) {
9986 		uint32_t    wordA;
9987 
9988 		IOLockLock(pmTraceWorkerLock);
9989 		wordA = (loginWindowData << 24) | (coreDisplayData << 16) |
9990 		    (coreGraphicsData << 8) | tracePhase;
9991 		IOLockUnlock(pmTraceWorkerLock);
9992 
9993 		tracePointHandler( tracePointTarget, traceData32, wordA );
9994 		_LOG("RTC_TRACE wrote 0x%08x 0x%08x\n", traceData32, wordA);
9995 	}
9996 #if DEVELOPMENT || DEBUG
9997 	if ((swd_panic_phase != 0) && (swd_panic_phase == tracePhase)) {
9998 		DEBUG_LOG("Causing sleep wake failure in phase 0x%08x\n", tracePhase);
9999 		IOLock *l = IOLockAlloc();
10000 		IOLockLock(l);
10001 		IOLockLock(l);
10002 	}
10003 #endif /* DEVELOPMENT || DEBUG */
10004 }
10005 
10006 int
10007 PMTraceWorker::recordTopLevelPCIDevice(IOService * pciDevice)
10008 {
10009 	OSSharedPtr<const OSSymbol>    deviceName;
10010 	int                 index = -1;
10011 
10012 	IOLockLock(pmTraceWorkerLock);
10013 
10014 	if (!pciDeviceBitMappings) {
10015 		pciDeviceBitMappings = OSArray::withCapacity(kPMBestGuessPCIDevicesCount);
10016 		if (!pciDeviceBitMappings) {
10017 			goto exit;
10018 		}
10019 	}
10020 
10021 	// Check for bitmask overflow.
10022 	if (pciDeviceBitMappings->getCount() >= kPMMaxRTCBitfieldSize) {
10023 		goto exit;
10024 	}
10025 
10026 	if ((deviceName = pciDevice->copyName()) &&
10027 	    (pciDeviceBitMappings->getNextIndexOfObject(deviceName.get(), 0) == (unsigned int)-1) &&
10028 	    pciDeviceBitMappings->setObject(deviceName.get())) {
10029 		index = pciDeviceBitMappings->getCount() - 1;
10030 		_LOG("PMTrace PCI array: set object %s => %d\n",
10031 		    deviceName->getCStringNoCopy(), index);
10032 	}
10033 
10034 	if (!addedToRegistry && (index >= 0)) {
10035 		addedToRegistry = owner->setProperty("PCITopLevel", this);
10036 	}
10037 
10038 exit:
10039 	IOLockUnlock(pmTraceWorkerLock);
10040 	return index;
10041 }
10042 
10043 bool
10044 PMTraceWorker::serialize(OSSerialize *s) const
10045 {
10046 	bool ok = false;
10047 	if (pciDeviceBitMappings) {
10048 		IOLockLock(pmTraceWorkerLock);
10049 		ok = pciDeviceBitMappings->serialize(s);
10050 		IOLockUnlock(pmTraceWorkerLock);
10051 	}
10052 	return ok;
10053 }
10054 
10055 void
10056 PMTraceWorker::tracePoint(uint8_t phase)
10057 {
10058 	// clear trace detail when phase begins
10059 	if (tracePhase != phase) {
10060 		traceData32 = 0;
10061 	}
10062 
10063 	tracePhase = phase;
10064 
10065 	DLOG("trace point 0x%02x\n", tracePhase);
10066 	RTC_TRACE();
10067 }
10068 
10069 void
10070 PMTraceWorker::traceDetail(uint32_t detail)
10071 {
10072 	if (detail == traceData32) {
10073 		return;
10074 	}
10075 	traceData32 = detail;
10076 	RTC_TRACE();
10077 }
10078 
10079 void
10080 PMTraceWorker::traceComponentWakeProgress(uint32_t component, uint32_t data)
10081 {
10082 	switch (component) {
10083 	case kIOPMLoginWindowProgress:
10084 		loginWindowData = data & kIOPMLoginWindowProgressMask;
10085 		break;
10086 	case kIOPMCoreDisplayProgress:
10087 		coreDisplayData = data & kIOPMCoreDisplayProgressMask;
10088 		break;
10089 	case kIOPMCoreGraphicsProgress:
10090 		coreGraphicsData = data & kIOPMCoreGraphicsProgressMask;
10091 		break;
10092 	default:
10093 		return;
10094 	}
10095 
10096 	DLOG("component trace point 0x%02x data 0x%08x\n", component, data);
10097 	RTC_TRACE();
10098 }
10099 
10100 void
10101 PMTraceWorker::tracePCIPowerChange(
10102 	change_t type, IOService *service, uint32_t changeFlags, uint32_t bitNum)
10103 {
10104 	uint32_t    bitMask;
10105 	uint32_t    expectedFlag;
10106 
10107 	// Ignore PCI changes outside of system sleep/wake.
10108 	if ((kIOPMTracePointSleepPowerPlaneDrivers != tracePhase) &&
10109 	    (kIOPMTracePointWakePowerPlaneDrivers != tracePhase)) {
10110 		return;
10111 	}
10112 
10113 	// Only record the WillChange transition when going to sleep,
10114 	// and the DidChange on the way up.
10115 	changeFlags &= (kIOPMDomainWillChange | kIOPMDomainDidChange);
10116 	expectedFlag = (kIOPMTracePointSleepPowerPlaneDrivers == tracePhase) ?
10117 	    kIOPMDomainWillChange : kIOPMDomainDidChange;
10118 	if (changeFlags != expectedFlag) {
10119 		return;
10120 	}
10121 
10122 	// Mark this device off in our bitfield
10123 	if (bitNum < kPMMaxRTCBitfieldSize) {
10124 		bitMask = (1 << bitNum);
10125 
10126 		if (kPowerChangeStart == type) {
10127 			traceData32 |= bitMask;
10128 			_LOG("PMTrace: Device %s started  - bit %2d mask 0x%08x => 0x%08x\n",
10129 			    service->getName(), bitNum, bitMask, traceData32);
10130 			owner->kdebugTrace(kPMLogPCIDevChangeStart, service->getRegistryEntryID(), traceData32, 0);
10131 		} else {
10132 			traceData32 &= ~bitMask;
10133 			_LOG("PMTrace: Device %s finished - bit %2d mask 0x%08x => 0x%08x\n",
10134 			    service->getName(), bitNum, bitMask, traceData32);
10135 			owner->kdebugTrace(kPMLogPCIDevChangeDone, service->getRegistryEntryID(), traceData32, 0);
10136 		}
10137 
10138 		DLOG("trace point 0x%02x detail 0x%08x\n", tracePhase, traceData32);
10139 		RTC_TRACE();
10140 	}
10141 }
10142 
10143 uint64_t
10144 PMTraceWorker::getPMStatusCode()
10145 {
10146 	return ((uint64_t)traceData32 << 32) | ((uint64_t)tracePhase);
10147 }
10148 
10149 uint8_t
10150 PMTraceWorker::getTracePhase()
10151 {
10152 	return tracePhase;
10153 }
10154 
10155 uint32_t
10156 PMTraceWorker::getTraceData()
10157 {
10158 	return traceData32;
10159 }
10160 
10161 // MARK: -
10162 // MARK: PMHaltWorker
10163 
10164 //******************************************************************************
10165 // PMHaltWorker Class
10166 //
10167 //******************************************************************************
10168 
10169 PMHaltWorker *
10170 PMHaltWorker::worker( void )
10171 {
10172 	PMHaltWorker *  me;
10173 	IOThread        thread;
10174 
10175 	do {
10176 		me = OSTypeAlloc( PMHaltWorker );
10177 		if (!me || !me->init()) {
10178 			break;
10179 		}
10180 
10181 		me->lock = IOLockAlloc();
10182 		if (!me->lock) {
10183 			break;
10184 		}
10185 
10186 		DLOG("PMHaltWorker %p\n", OBFUSCATE(me));
10187 		me->retain(); // thread holds extra retain
10188 		if (KERN_SUCCESS != kernel_thread_start(&PMHaltWorker::main, (void *) me, &thread)) {
10189 			me->release();
10190 			break;
10191 		}
10192 		thread_deallocate(thread);
10193 		return me;
10194 	} while (false);
10195 
10196 	if (me) {
10197 		me->release();
10198 	}
10199 	return NULL;
10200 }
10201 
10202 void
10203 PMHaltWorker::free( void )
10204 {
10205 	DLOG("PMHaltWorker free %p\n", OBFUSCATE(this));
10206 	if (lock) {
10207 		IOLockFree(lock);
10208 		lock = NULL;
10209 	}
10210 	return OSObject::free();
10211 }
10212 
10213 void
10214 PMHaltWorker::main( void * arg, wait_result_t waitResult )
10215 {
10216 	PMHaltWorker * me = (PMHaltWorker *) arg;
10217 
10218 	IOLockLock( gPMHaltLock );
10219 	gPMHaltBusyCount++;
10220 	me->depth = gPMHaltDepth;
10221 	IOLockUnlock( gPMHaltLock );
10222 
10223 	while (me->depth >= 0) {
10224 		PMHaltWorker::work( me );
10225 
10226 		IOLockLock( gPMHaltLock );
10227 		if (++gPMHaltIdleCount >= gPMHaltBusyCount) {
10228 			// This is the last thread to finish work on this level,
10229 			// inform everyone to start working on next lower level.
10230 			gPMHaltDepth--;
10231 			me->depth = gPMHaltDepth;
10232 			gPMHaltIdleCount = 0;
10233 			thread_wakeup((event_t) &gPMHaltIdleCount);
10234 		} else {
10235 			// One or more threads are still working on this level,
10236 			// this thread must wait.
10237 			me->depth = gPMHaltDepth - 1;
10238 			do {
10239 				IOLockSleep(gPMHaltLock, &gPMHaltIdleCount, THREAD_UNINT);
10240 			} while (me->depth != gPMHaltDepth);
10241 		}
10242 		IOLockUnlock( gPMHaltLock );
10243 	}
10244 
10245 	// No more work to do, terminate thread
10246 	DLOG("All done for worker: %p (visits = %u)\n", OBFUSCATE(me), me->visits);
10247 	thread_wakeup( &gPMHaltDepth );
10248 	me->release();
10249 }
10250 
10251 void
10252 PMHaltWorker::work( PMHaltWorker * me )
10253 {
10254 	OSSharedPtr<IOService>     service;
10255 	OSSet *         inner;
10256 	AbsoluteTime    startTime, elapsedTime;
10257 	UInt32          deltaTime;
10258 	bool            timeout;
10259 
10260 	while (true) {
10261 		timeout = false;
10262 
10263 		// Claim an unit of work from the shared pool
10264 		IOLockLock( gPMHaltLock );
10265 		inner = (OSSet *)gPMHaltArray->getObject(me->depth);
10266 		if (inner) {
10267 			service.reset(OSDynamicCast(IOService, inner->getAnyObject()), OSRetain);
10268 			if (service) {
10269 				inner->removeObject(service.get());
10270 			}
10271 		}
10272 		IOLockUnlock( gPMHaltLock );
10273 		if (!service) {
10274 			break; // no more work at this depth
10275 		}
10276 		clock_get_uptime(&startTime);
10277 
10278 		if (!service->isInactive() &&
10279 		    service->setProperty(gPMHaltClientAcknowledgeKey.get(), me)) {
10280 			IOLockLock(me->lock);
10281 			me->startTime = startTime;
10282 			me->service   = service.get();
10283 			me->timeout   = false;
10284 			IOLockUnlock(me->lock);
10285 
10286 			service->systemWillShutdown( gPMHaltMessageType);
10287 
10288 			// Wait for driver acknowledgement
10289 			IOLockLock(me->lock);
10290 			while (service->propertyExists(gPMHaltClientAcknowledgeKey.get())) {
10291 				IOLockSleep(me->lock, me, THREAD_UNINT);
10292 			}
10293 			me->service = NULL;
10294 			timeout = me->timeout;
10295 			IOLockUnlock(me->lock);
10296 		}
10297 
10298 		deltaTime = computeDeltaTimeMS(&startTime, &elapsedTime);
10299 		if ((deltaTime > kPMHaltTimeoutMS) || timeout) {
10300 			LOG("%s driver %s (0x%llx) took %u ms\n",
10301 			    (gPMHaltMessageType == kIOMessageSystemWillPowerOff) ?
10302 			    "PowerOff" : "Restart",
10303 			    service->getName(), service->getRegistryEntryID(),
10304 			    (uint32_t) deltaTime );
10305 			halt_log_enter("PowerOff/Restart handler completed",
10306 			    OSMemberFunctionCast(const void *, service.get(), &IOService::systemWillShutdown),
10307 			    elapsedTime);
10308 		}
10309 
10310 		me->visits++;
10311 	}
10312 }
10313 
10314 void
10315 PMHaltWorker::checkTimeout( PMHaltWorker * me, AbsoluteTime * now )
10316 {
10317 	UInt64          nano;
10318 	AbsoluteTime    startTime;
10319 	AbsoluteTime    endTime;
10320 
10321 	endTime = *now;
10322 
10323 	IOLockLock(me->lock);
10324 	if (me->service && !me->timeout) {
10325 		startTime = me->startTime;
10326 		nano = 0;
10327 		if (CMP_ABSOLUTETIME(&endTime, &startTime) > 0) {
10328 			SUB_ABSOLUTETIME(&endTime, &startTime);
10329 			absolutetime_to_nanoseconds(endTime, &nano);
10330 		}
10331 		if (nano > 3000000000ULL) {
10332 			me->timeout = true;
10333 
10334 			halt_log_enter("PowerOff/Restart still waiting on handler",
10335 			    OSMemberFunctionCast(const void *, me->service, &IOService::systemWillShutdown),
10336 			    endTime);
10337 			MSG("%s still waiting on %s\n",
10338 			    (gPMHaltMessageType == kIOMessageSystemWillPowerOff) ?  "PowerOff" : "Restart",
10339 			    me->service->getName());
10340 		}
10341 	}
10342 	IOLockUnlock(me->lock);
10343 }
10344 
10345 //******************************************************************************
10346 // acknowledgeSystemWillShutdown
10347 //
10348 // Acknowledgement from drivers that they have prepared for shutdown/restart.
10349 //******************************************************************************
10350 
10351 void
10352 IOPMrootDomain::acknowledgeSystemWillShutdown( IOService * from )
10353 {
10354 	PMHaltWorker            * worker;
10355 	OSSharedPtr<OSObject>     prop;
10356 
10357 	if (!from) {
10358 		return;
10359 	}
10360 
10361 	//DLOG("%s acknowledged\n", from->getName());
10362 	prop = from->copyProperty( gPMHaltClientAcknowledgeKey.get());
10363 	if (prop) {
10364 		worker = (PMHaltWorker *) prop.get();
10365 		IOLockLock(worker->lock);
10366 		from->removeProperty( gPMHaltClientAcknowledgeKey.get());
10367 		thread_wakeup((event_t) worker);
10368 		IOLockUnlock(worker->lock);
10369 	} else {
10370 		DLOG("%s acknowledged without worker property\n",
10371 		    from->getName());
10372 	}
10373 }
10374 
10375 
10376 //******************************************************************************
10377 // notifySystemShutdown
10378 //
10379 // Notify all objects in PM tree that system will shutdown or restart
10380 //******************************************************************************
10381 
10382 static void
10383 notifySystemShutdown( IOService * root, uint32_t messageType )
10384 {
10385 #define PLACEHOLDER ((OSSet *)gPMHaltArray.get())
10386 	OSSharedPtr<IORegistryIterator>  iter;
10387 	IORegistryEntry *                entry;
10388 	IOService *                      node;
10389 	OSSet *                          inner;
10390 	OSSharedPtr<OSSet>               newInner;
10391 	PMHaltWorker *                   workers[kPMHaltMaxWorkers];
10392 	AbsoluteTime                     deadline;
10393 	unsigned int                     totalNodes = 0;
10394 	unsigned int                     depth;
10395 	unsigned int                     rootDepth;
10396 	unsigned int                     numWorkers;
10397 	unsigned int                     count;
10398 	int                              waitResult;
10399 	void *                           baseFunc;
10400 	bool                             ok;
10401 
10402 	DLOG("%s msgType = 0x%x\n", __FUNCTION__, messageType);
10403 
10404 	baseFunc = OSMemberFunctionCast(void *, root, &IOService::systemWillShutdown);
10405 
10406 	// Iterate the entire PM tree starting from root
10407 
10408 	rootDepth = root->getDepth( gIOPowerPlane );
10409 	if (!rootDepth) {
10410 		goto done;
10411 	}
10412 
10413 	// debug - for repeated test runs
10414 	while (PMHaltWorker::metaClass->getInstanceCount()) {
10415 		IOSleep(1);
10416 	}
10417 
10418 	if (!gPMHaltArray) {
10419 		gPMHaltArray = OSArray::withCapacity(40);
10420 		if (!gPMHaltArray) {
10421 			goto done;
10422 		}
10423 	} else { // debug
10424 		gPMHaltArray->flushCollection();
10425 	}
10426 
10427 	if (!gPMHaltLock) {
10428 		gPMHaltLock = IOLockAlloc();
10429 		if (!gPMHaltLock) {
10430 			goto done;
10431 		}
10432 	}
10433 
10434 	if (!gPMHaltClientAcknowledgeKey) {
10435 		gPMHaltClientAcknowledgeKey =
10436 		    OSSymbol::withCStringNoCopy("PMShutdown");
10437 		if (!gPMHaltClientAcknowledgeKey) {
10438 			goto done;
10439 		}
10440 	}
10441 
10442 	gPMHaltMessageType = messageType;
10443 
10444 	// Depth-first walk of PM plane
10445 
10446 	iter = IORegistryIterator::iterateOver(
10447 		root, gIOPowerPlane, kIORegistryIterateRecursively);
10448 
10449 	if (iter) {
10450 		while ((entry = iter->getNextObject())) {
10451 			node = OSDynamicCast(IOService, entry);
10452 			if (!node) {
10453 				continue;
10454 			}
10455 
10456 			if (baseFunc ==
10457 			    OSMemberFunctionCast(void *, node, &IOService::systemWillShutdown)) {
10458 				continue;
10459 			}
10460 
10461 			depth = node->getDepth( gIOPowerPlane );
10462 			if (depth <= rootDepth) {
10463 				continue;
10464 			}
10465 
10466 			ok = false;
10467 
10468 			// adjust to zero based depth
10469 			depth -= (rootDepth + 1);
10470 
10471 			// gPMHaltArray is an array of containers, each container
10472 			// refers to nodes with the same depth.
10473 
10474 			count = gPMHaltArray->getCount();
10475 			while (depth >= count) {
10476 				// expand array and insert placeholders
10477 				gPMHaltArray->setObject(PLACEHOLDER);
10478 				count++;
10479 			}
10480 			count = gPMHaltArray->getCount();
10481 			if (depth < count) {
10482 				inner = (OSSet *)gPMHaltArray->getObject(depth);
10483 				if (inner == PLACEHOLDER) {
10484 					newInner = OSSet::withCapacity(40);
10485 					if (newInner) {
10486 						gPMHaltArray->replaceObject(depth, newInner.get());
10487 						inner = newInner.get();
10488 					}
10489 				}
10490 
10491 				// PM nodes that appear more than once in the tree will have
10492 				// the same depth, OSSet will refuse to add the node twice.
10493 				if (inner) {
10494 					ok = inner->setObject(node);
10495 				}
10496 			}
10497 			if (!ok) {
10498 				DLOG("Skipped PM node %s\n", node->getName());
10499 			}
10500 		}
10501 	}
10502 
10503 	// debug only
10504 	for (int i = 0; (inner = (OSSet *)gPMHaltArray->getObject(i)); i++) {
10505 		count = 0;
10506 		if (inner != PLACEHOLDER) {
10507 			count = inner->getCount();
10508 		}
10509 		DLOG("Nodes at depth %u = %u\n", i, count);
10510 	}
10511 
10512 	// strip placeholders (not all depths are populated)
10513 	numWorkers = 0;
10514 	for (int i = 0; (inner = (OSSet *)gPMHaltArray->getObject(i));) {
10515 		if (inner == PLACEHOLDER) {
10516 			gPMHaltArray->removeObject(i);
10517 			continue;
10518 		}
10519 		count = inner->getCount();
10520 		if (count > numWorkers) {
10521 			numWorkers = count;
10522 		}
10523 		totalNodes += count;
10524 		i++;
10525 	}
10526 
10527 	if (gPMHaltArray->getCount() == 0 || !numWorkers) {
10528 		goto done;
10529 	}
10530 
10531 	gPMHaltBusyCount = 0;
10532 	gPMHaltIdleCount = 0;
10533 	gPMHaltDepth = gPMHaltArray->getCount() - 1;
10534 
10535 	// Create multiple workers (and threads)
10536 
10537 	if (numWorkers > kPMHaltMaxWorkers) {
10538 		numWorkers = kPMHaltMaxWorkers;
10539 	}
10540 
10541 	DLOG("PM nodes %u, maxDepth %u, workers %u\n",
10542 	    totalNodes, gPMHaltArray->getCount(), numWorkers);
10543 
10544 	for (unsigned int i = 0; i < numWorkers; i++) {
10545 		workers[i] = PMHaltWorker::worker();
10546 	}
10547 
10548 	// Wait for workers to exhaust all available work
10549 
10550 	IOLockLock(gPMHaltLock);
10551 	while (gPMHaltDepth >= 0) {
10552 		clock_interval_to_deadline(1000, kMillisecondScale, &deadline);
10553 
10554 		waitResult = IOLockSleepDeadline(
10555 			gPMHaltLock, &gPMHaltDepth, deadline, THREAD_UNINT);
10556 		if (THREAD_TIMED_OUT == waitResult) {
10557 			AbsoluteTime now;
10558 			clock_get_uptime(&now);
10559 
10560 			IOLockUnlock(gPMHaltLock);
10561 			for (unsigned int i = 0; i < numWorkers; i++) {
10562 				if (workers[i]) {
10563 					PMHaltWorker::checkTimeout(workers[i], &now);
10564 				}
10565 			}
10566 			IOLockLock(gPMHaltLock);
10567 		}
10568 	}
10569 	IOLockUnlock(gPMHaltLock);
10570 
10571 	// Release all workers
10572 
10573 	for (unsigned int i = 0; i < numWorkers; i++) {
10574 		if (workers[i]) {
10575 			workers[i]->release();
10576 		}
10577 		// worker also retained by it's own thread
10578 	}
10579 
10580 done:
10581 	DLOG("%s done\n", __FUNCTION__);
10582 	return;
10583 }
10584 
10585 // MARK: -
10586 // MARK: Kernel Assertion
10587 
10588 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
10589 
10590 IOPMDriverAssertionID
10591 IOPMrootDomain::createPMAssertion(
10592 	IOPMDriverAssertionType whichAssertionBits,
10593 	IOPMDriverAssertionLevel assertionLevel,
10594 	IOService *ownerService,
10595 	const char *ownerDescription)
10596 {
10597 	IOReturn            ret;
10598 	IOPMDriverAssertionID     newAssertion;
10599 
10600 	if (!pmAssertions) {
10601 		return 0;
10602 	}
10603 
10604 	ret = pmAssertions->createAssertion(whichAssertionBits, assertionLevel, ownerService, ownerDescription, &newAssertion);
10605 
10606 	if (kIOReturnSuccess == ret) {
10607 #if (DEVELOPMENT || DEBUG)
10608 		if (_aotNow || (kIOLogPMRootDomain & gIOKitDebug)) {
10609 			const char *serviceName = (ownerService && ownerService->reserved) ? ownerService->getName() : NULL;
10610 			OSReportWithBacktrace("PMRD: createPMAssertion(0x%qx) %s (%s)", newAssertion,
10611 			    serviceName, ownerDescription);
10612 		}
10613 #endif /* (DEVELOPMENT || DEBUG) */
10614 		return newAssertion;
10615 	} else {
10616 		return 0;
10617 	}
10618 }
10619 
10620 IOReturn
10621 IOPMrootDomain::releasePMAssertion(IOPMDriverAssertionID releaseAssertion)
10622 {
10623 #if (DEVELOPMENT || DEBUG)
10624 	if (_aotNow || (kIOLogPMRootDomain & gIOKitDebug)) {
10625 		PMAssertStruct *details = pmAssertions->detailsForID(releaseAssertion, NULL);
10626 		if (details) {
10627 			const char *serviceName = (details->ownerService && details->ownerService->reserved) ?
10628 			    details->ownerService->getName() : NULL;
10629 			const char *ownerString = details->ownerString ? details->ownerString->getCStringNoCopy() : NULL;
10630 			OSReportWithBacktrace("PMRD: releasePMAssertion(0x%qx) %s (%s)", releaseAssertion, serviceName, ownerString);
10631 		} else {
10632 			OSReportWithBacktrace("PMRD: releasePMAssertion(0x%qx)", releaseAssertion);
10633 		}
10634 	}
10635 #endif /* (DEVELOPMENT || DEBUG) */
10636 	if (!pmAssertions) {
10637 		return kIOReturnInternalError;
10638 	}
10639 	return pmAssertions->releaseAssertion(releaseAssertion);
10640 }
10641 
10642 
10643 IOReturn
10644 IOPMrootDomain::setPMAssertionLevel(
10645 	IOPMDriverAssertionID assertionID,
10646 	IOPMDriverAssertionLevel assertionLevel)
10647 {
10648 	return pmAssertions->setAssertionLevel(assertionID, assertionLevel);
10649 }
10650 
10651 IOPMDriverAssertionLevel
10652 IOPMrootDomain::getPMAssertionLevel(IOPMDriverAssertionType whichAssertion)
10653 {
10654 	IOPMDriverAssertionType       sysLevels;
10655 
10656 	if (!pmAssertions || whichAssertion == 0) {
10657 		return kIOPMDriverAssertionLevelOff;
10658 	}
10659 
10660 	sysLevels = pmAssertions->getActivatedAssertions();
10661 
10662 	// Check that every bit set in argument 'whichAssertion' is asserted
10663 	// in the aggregate bits.
10664 	if ((sysLevels & whichAssertion) == whichAssertion) {
10665 		return kIOPMDriverAssertionLevelOn;
10666 	} else {
10667 		return kIOPMDriverAssertionLevelOff;
10668 	}
10669 }
10670 
10671 IOReturn
10672 IOPMrootDomain::setPMAssertionUserLevels(IOPMDriverAssertionType inLevels)
10673 {
10674 	if (!pmAssertions) {
10675 		return kIOReturnNotFound;
10676 	}
10677 
10678 	return pmAssertions->setUserAssertionLevels(inLevels);
10679 }
10680 
10681 IOReturn
10682 IOPMrootDomain::acquireDriverKitMatchingAssertion()
10683 {
10684 	return gIOPMWorkLoop->runActionBlock(^{
10685 		if (_driverKitMatchingAssertionCount != 0) {
10686 		        _driverKitMatchingAssertionCount++;
10687 		        return kIOReturnSuccess;
10688 		} else {
10689 		        if (kSystemTransitionSleep == _systemTransitionType) {
10690 		                // system going to sleep
10691 		                return kIOReturnBusy;
10692 			} else {
10693 		                // createPMAssertion is asynchronous.
10694 		                // we must also set _driverKitMatchingAssertionCount under the PM workloop lock so that we can cancel sleep immediately
10695 		                // The assertion is used so that on release, we reevaluate all assertions
10696 		                _driverKitMatchingAssertion = createPMAssertion(kIOPMDriverAssertionCPUBit, kIOPMDriverAssertionLevelOn, this, "DK matching");
10697 		                if (_driverKitMatchingAssertion != kIOPMUndefinedDriverAssertionID) {
10698 		                        _driverKitMatchingAssertionCount = 1;
10699 		                        return kIOReturnSuccess;
10700 				} else {
10701 		                        return kIOReturnBusy;
10702 				}
10703 			}
10704 		}
10705 	});
10706 }
10707 
10708 void
10709 IOPMrootDomain::releaseDriverKitMatchingAssertion()
10710 {
10711 	gIOPMWorkLoop->runActionBlock(^{
10712 		if (_driverKitMatchingAssertionCount != 0) {
10713 		        _driverKitMatchingAssertionCount--;
10714 		        if (_driverKitMatchingAssertionCount == 0) {
10715 		                releasePMAssertion(_driverKitMatchingAssertion);
10716 		                _driverKitMatchingAssertion = kIOPMUndefinedDriverAssertionID;
10717 			}
10718 		} else {
10719 		        panic("Over-release of driverkit matching assertion");
10720 		}
10721 		return kIOReturnSuccess;
10722 	});
10723 }
10724 
10725 bool
10726 IOPMrootDomain::serializeProperties( OSSerialize * s ) const
10727 {
10728 	if (pmAssertions) {
10729 		pmAssertions->publishProperties();
10730 	}
10731 	return IOService::serializeProperties(s);
10732 }
10733 
10734 OSSharedPtr<OSObject>
10735 IOPMrootDomain::copyProperty( const char * aKey) const
10736 {
10737 	OSSharedPtr<OSObject> obj;
10738 	obj = IOService::copyProperty(aKey);
10739 
10740 	if (obj) {
10741 		return obj;
10742 	}
10743 
10744 	if (!strncmp(aKey, kIOPMSleepWakeWdogRebootKey,
10745 	    sizeof(kIOPMSleepWakeWdogRebootKey))) {
10746 		if (swd_flags & SWD_BOOT_BY_SW_WDOG) {
10747 			return OSSharedPtr<OSBoolean>(kOSBooleanTrue, OSNoRetain);
10748 		} else {
10749 			return OSSharedPtr<OSBoolean>(kOSBooleanFalse, OSNoRetain);
10750 		}
10751 	}
10752 
10753 	if (!strncmp(aKey, kIOPMSleepWakeWdogLogsValidKey,
10754 	    sizeof(kIOPMSleepWakeWdogLogsValidKey))) {
10755 		if (swd_flags & SWD_VALID_LOGS) {
10756 			return OSSharedPtr<OSBoolean>(kOSBooleanTrue, OSNoRetain);
10757 		} else {
10758 			return OSSharedPtr<OSBoolean>(kOSBooleanFalse, OSNoRetain);
10759 		}
10760 	}
10761 
10762 	/*
10763 	 * XXX: We should get rid of "DesktopMode" property  when 'kAppleClamshellCausesSleepKey'
10764 	 * is set properly in darwake from sleep. For that, kIOPMEnableClamshell msg has to be
10765 	 * issued by DisplayWrangler on darkwake.
10766 	 */
10767 	if (!strcmp(aKey, "DesktopMode")) {
10768 		if (desktopMode) {
10769 			return OSSharedPtr<OSBoolean>(kOSBooleanTrue, OSNoRetain);
10770 		} else {
10771 			return OSSharedPtr<OSBoolean>(kOSBooleanFalse, OSNoRetain);
10772 		}
10773 	}
10774 	if (!strcmp(aKey, "DisplayIdleForDemandSleep")) {
10775 		if (displayIdleForDemandSleep) {
10776 			return OSSharedPtr<OSBoolean>(kOSBooleanTrue, OSNoRetain);
10777 		} else {
10778 			return OSSharedPtr<OSBoolean>(kOSBooleanFalse, OSNoRetain);
10779 		}
10780 	}
10781 
10782 	if (!strcmp(aKey, kIOPMDriverWakeEventsKey)) {
10783 		OSSharedPtr<OSArray> array;
10784 		WAKEEVENT_LOCK();
10785 		if (_systemWakeEventsArray && _systemWakeEventsArray->getCount()) {
10786 			OSSharedPtr<OSCollection> collection = _systemWakeEventsArray->copyCollection();
10787 			if (collection) {
10788 				array = OSDynamicPtrCast<OSArray>(collection);
10789 			}
10790 		}
10791 		WAKEEVENT_UNLOCK();
10792 		return os::move(array);
10793 	}
10794 
10795 	if (!strcmp(aKey, kIOPMSleepStatisticsAppsKey)) {
10796 		OSSharedPtr<OSArray> array;
10797 		IOLockLock(pmStatsLock);
10798 		if (pmStatsAppResponses && pmStatsAppResponses->getCount()) {
10799 			OSSharedPtr<OSCollection> collection = pmStatsAppResponses->copyCollection();
10800 			if (collection) {
10801 				array = OSDynamicPtrCast<OSArray>(collection);
10802 			}
10803 		}
10804 		IOLockUnlock(pmStatsLock);
10805 		return os::move(array);
10806 	}
10807 
10808 	if (!strcmp(aKey, kIOPMIdleSleepPreventersKey)) {
10809 		OSArray *idleSleepList = NULL;
10810 		gRootDomain->copySleepPreventersList(&idleSleepList, NULL);
10811 		return OSSharedPtr<OSArray>(idleSleepList, OSNoRetain);
10812 	}
10813 
10814 	if (!strcmp(aKey, kIOPMSystemSleepPreventersKey)) {
10815 		OSArray *systemSleepList = NULL;
10816 		gRootDomain->copySleepPreventersList(NULL, &systemSleepList);
10817 		return OSSharedPtr<OSArray>(systemSleepList, OSNoRetain);
10818 	}
10819 
10820 	if (!strcmp(aKey, kIOPMIdleSleepPreventersWithIDKey)) {
10821 		OSArray *idleSleepList = NULL;
10822 		gRootDomain->copySleepPreventersListWithID(&idleSleepList, NULL);
10823 		return OSSharedPtr<OSArray>(idleSleepList, OSNoRetain);
10824 	}
10825 
10826 	if (!strcmp(aKey, kIOPMSystemSleepPreventersWithIDKey)) {
10827 		OSArray *systemSleepList = NULL;
10828 		gRootDomain->copySleepPreventersListWithID(NULL, &systemSleepList);
10829 		return OSSharedPtr<OSArray>(systemSleepList, OSNoRetain);
10830 	}
10831 	return NULL;
10832 }
10833 
10834 // MARK: -
10835 // MARK: Wake Event Reporting
10836 
10837 void
10838 IOPMrootDomain::copyWakeReasonString( char * outBuf, size_t bufSize )
10839 {
10840 	WAKEEVENT_LOCK();
10841 	strlcpy(outBuf, gWakeReasonString, bufSize);
10842 	WAKEEVENT_UNLOCK();
10843 }
10844 
10845 void
10846 IOPMrootDomain::copyShutdownReasonString( char * outBuf, size_t bufSize )
10847 {
10848 	WAKEEVENT_LOCK();
10849 	strlcpy(outBuf, gShutdownReasonString, bufSize);
10850 	WAKEEVENT_UNLOCK();
10851 }
10852 
10853 //******************************************************************************
10854 // acceptSystemWakeEvents
10855 //
10856 // Private control for the acceptance of driver wake event claims.
10857 //******************************************************************************
10858 
10859 void
10860 IOPMrootDomain::acceptSystemWakeEvents( uint32_t control )
10861 {
10862 	bool logWakeReason = false;
10863 
10864 	WAKEEVENT_LOCK();
10865 	switch (control) {
10866 	case kAcceptSystemWakeEvents_Enable:
10867 		assert(_acceptSystemWakeEvents == false);
10868 		if (!_systemWakeEventsArray) {
10869 			_systemWakeEventsArray = OSArray::withCapacity(4);
10870 		}
10871 		_acceptSystemWakeEvents = (_systemWakeEventsArray != NULL);
10872 		if (!(_aotNow && (kIOPMWakeEventAOTExitFlags & _aotPendingFlags))) {
10873 			gWakeReasonString[0] = '\0';
10874 			if (_systemWakeEventsArray) {
10875 				_systemWakeEventsArray->flushCollection();
10876 			}
10877 		}
10878 
10879 		// Remove stale WakeType property before system sleep
10880 		removeProperty(kIOPMRootDomainWakeTypeKey);
10881 		removeProperty(kIOPMRootDomainWakeReasonKey);
10882 		break;
10883 
10884 	case kAcceptSystemWakeEvents_Disable:
10885 		_acceptSystemWakeEvents = false;
10886 #if defined(XNU_TARGET_OS_OSX)
10887 		logWakeReason = (gWakeReasonString[0] != '\0');
10888 #else /* !defined(XNU_TARGET_OS_OSX) */
10889 		logWakeReason = gWakeReasonSysctlRegistered;
10890 #if DEVELOPMENT
10891 		static int panic_allowed = -1;
10892 
10893 		if ((panic_allowed == -1) &&
10894 		    (PE_parse_boot_argn("swd_wakereason_panic", &panic_allowed, sizeof(panic_allowed)) == false)) {
10895 			panic_allowed = 0;
10896 		}
10897 
10898 		if (panic_allowed) {
10899 			size_t i = 0;
10900 			// Panic if wake reason is null or empty
10901 			for (i = 0; (i < strlen(gWakeReasonString)); i++) {
10902 				if ((gWakeReasonString[i] != ' ') && (gWakeReasonString[i] != '\t')) {
10903 					break;
10904 				}
10905 			}
10906 			if (i >= strlen(gWakeReasonString)) {
10907 				panic("Wake reason is empty");
10908 			}
10909 		}
10910 #endif /* DEVELOPMENT */
10911 #endif /* !defined(XNU_TARGET_OS_OSX) */
10912 
10913 		// publish kIOPMRootDomainWakeReasonKey if not already set
10914 		if (!propertyExists(kIOPMRootDomainWakeReasonKey)) {
10915 			setProperty(kIOPMRootDomainWakeReasonKey, gWakeReasonString);
10916 		}
10917 		break;
10918 
10919 	case kAcceptSystemWakeEvents_Reenable:
10920 		assert(_acceptSystemWakeEvents == false);
10921 		_acceptSystemWakeEvents = (_systemWakeEventsArray != NULL);
10922 		removeProperty(kIOPMRootDomainWakeReasonKey);
10923 		break;
10924 	}
10925 	WAKEEVENT_UNLOCK();
10926 
10927 	if (logWakeReason) {
10928 		MSG("system wake events: %s\n", gWakeReasonString);
10929 	}
10930 }
10931 
10932 //******************************************************************************
10933 // claimSystemWakeEvent
10934 //
10935 // For a driver to claim a device is the source/conduit of a system wake event.
10936 //******************************************************************************
10937 
10938 void
10939 IOPMrootDomain::claimSystemWakeEvent(
10940 	IOService *     device,
10941 	IOOptionBits    flags,
10942 	const char *    reason,
10943 	OSObject *      details )
10944 {
10945 	OSSharedPtr<const OSSymbol>     deviceName;
10946 	OSSharedPtr<OSNumber>           deviceRegId;
10947 	OSSharedPtr<OSNumber>           claimTime;
10948 	OSSharedPtr<OSData>             flagsData;
10949 	OSSharedPtr<OSString>           reasonString;
10950 	OSSharedPtr<OSDictionary>       dict;
10951 	uint64_t                        timestamp;
10952 	bool                            addWakeReason;
10953 
10954 	if (!device || !reason) {
10955 		return;
10956 	}
10957 
10958 	pmEventTimeStamp(&timestamp);
10959 
10960 	uint64_t args[3] = {};
10961 	strlcpy((char *)args, reason, sizeof(args));
10962 	kdebugTrace(kPMLogClaimSystemWake, args[0], args[1], args[2], device->getRegistryEntryID());
10963 
10964 	IOOptionBits        aotFlags = 0;
10965 	bool                needAOTEvaluate = FALSE;
10966 
10967 	if (kIOPMAOTModeAddEventFlags & _aotMode) {
10968 		// Only allow lingering in AOT_STATE for the two wake reasons used for the wrist raise gesture.
10969 		if (strcmp("AOP.OutboxNotEmpty", reason) && strcmp("spu_gesture", reason)) {
10970 			flags |= kIOPMWakeEventAOTExit;
10971 		}
10972 	}
10973 
10974 #if DEVELOPMENT || DEBUG
10975 	if (_aotLingerTime && !strcmp("rtc", reason)) {
10976 		flags |= kIOPMWakeEventAOTPossibleExit;
10977 	}
10978 #endif /* DEVELOPMENT || DEBUG */
10979 
10980 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
10981 	// Publishing the WakeType is serialized by the PM work loop
10982 	if (!strcmp("rtc", reason) && (_nextScheduledAlarmType != NULL)) {
10983 		pmPowerStateQueue->submitPowerEvent(kPowerEventPublishWakeType,
10984 		    (void *) _nextScheduledAlarmType.get());
10985 	}
10986 
10987 	// Workaround for the missing wake HID event
10988 	if (gDarkWakeFlags & kDarkWakeFlagUserWakeWorkaround) {
10989 		if (!strcmp("trackpadkeyboard", reason)) {
10990 			pmPowerStateQueue->submitPowerEvent(kPowerEventPublishWakeType,
10991 			    (void *) gIOPMWakeTypeUserKey.get());
10992 		}
10993 	}
10994 #endif
10995 
10996 	deviceName   = device->copyName(gIOServicePlane);
10997 	deviceRegId  = OSNumber::withNumber(device->getRegistryEntryID(), 64);
10998 	claimTime    = OSNumber::withNumber(timestamp, 64);
10999 	flagsData    = OSData::withValue(flags);
11000 	reasonString = OSString::withCString(reason);
11001 	dict = OSDictionary::withCapacity(5 + (details ? 1 : 0));
11002 	if (!dict || !deviceName || !deviceRegId || !claimTime || !flagsData || !reasonString) {
11003 		goto done;
11004 	}
11005 
11006 	dict->setObject(gIONameKey, deviceName.get());
11007 	dict->setObject(gIORegistryEntryIDKey, deviceRegId.get());
11008 	dict->setObject(kIOPMWakeEventTimeKey, claimTime.get());
11009 	dict->setObject(kIOPMWakeEventFlagsKey, flagsData.get());
11010 	dict->setObject(kIOPMWakeEventReasonKey, reasonString.get());
11011 	if (details) {
11012 		dict->setObject(kIOPMWakeEventDetailsKey, details);
11013 	}
11014 
11015 	WAKEEVENT_LOCK();
11016 	addWakeReason = _acceptSystemWakeEvents;
11017 	if (_aotMode) {
11018 		IOLog("claimSystemWakeEvent(%s, %s, 0x%x) 0x%x %d\n", reason, deviceName->getCStringNoCopy(), (int)flags, _aotPendingFlags, _aotReadyToFullWake);
11019 	}
11020 	aotFlags        = (kIOPMWakeEventAOTFlags & flags);
11021 	aotFlags        = (aotFlags & ~_aotPendingFlags);
11022 	needAOTEvaluate = false;
11023 	if (_aotNow && aotFlags) {
11024 		if (kIOPMWakeEventAOTPossibleExit & flags) {
11025 			_aotMetrics->possibleCount++;
11026 			_aotAnalytics->possibleCount++;
11027 		}
11028 		if (kIOPMWakeEventAOTConfirmedPossibleExit & flags) {
11029 			_aotMetrics->confirmedPossibleCount++;
11030 			_aotAnalytics->confirmedPossibleCount++;
11031 		}
11032 		if (kIOPMWakeEventAOTRejectedPossibleExit & flags) {
11033 			_aotMetrics->rejectedPossibleCount++;
11034 			_aotAnalytics->rejectedPossibleCount++;
11035 		}
11036 		if (kIOPMWakeEventAOTExpiredPossibleExit & flags) {
11037 			_aotMetrics->expiredPossibleCount++;
11038 			_aotAnalytics->expiredPossibleCount++;
11039 		}
11040 
11041 		_aotPendingFlags |= aotFlags;
11042 		addWakeReason     = _aotNow && _systemWakeEventsArray && ((kIOPMWakeEventAOTExitFlags & aotFlags));
11043 		needAOTEvaluate   = _aotReadyToFullWake;
11044 	}
11045 	DMSG("claimSystemWakeEvent(%s, 0x%x, %s, 0x%llx) aot %d phase 0x%x add %d\n",
11046 	    reason, (int)flags, deviceName->getCStringNoCopy(), device->getRegistryEntryID(),
11047 	    _aotNow, pmTracer->getTracePhase(), addWakeReason);
11048 
11049 #if DEVELOPMENT || DEBUG
11050 	if (addWakeReason) {
11051 		record_system_event(SYSTEM_EVENT_TYPE_INFO,
11052 		    SYSTEM_EVENT_SUBSYSTEM_PMRD,
11053 		    "Report System Wake Event",
11054 		    "Reason: %s Flags: 0x%x Device: %s, DeviceRegEntry: 0x%llx\n",
11055 		    reason,
11056 		    (int)flags,
11057 		    deviceName->getCStringNoCopy(),
11058 		    device->getRegistryEntryID()
11059 		    );
11060 	}
11061 #endif /* DEVELOPMENT || DEBUG */
11062 
11063 	if (!gWakeReasonSysctlRegistered) {
11064 		// Lazy registration until the platform driver stops registering
11065 		// the same name.
11066 		gWakeReasonSysctlRegistered = true;
11067 	}
11068 	if (addWakeReason) {
11069 		_systemWakeEventsArray->setObject(dict.get());
11070 		if (gWakeReasonString[0] != '\0') {
11071 			strlcat(gWakeReasonString, " ", sizeof(gWakeReasonString));
11072 		}
11073 		strlcat(gWakeReasonString, reason, sizeof(gWakeReasonString));
11074 	}
11075 
11076 	WAKEEVENT_UNLOCK();
11077 	if (needAOTEvaluate) {
11078 		// Call aotEvaluate() on PM work loop since it may call
11079 		// aotExit() which accesses PM state.
11080 		pmPowerStateQueue->submitPowerEvent(kPowerEventAOTEvaluate);
11081 	}
11082 
11083 done:
11084 	return;
11085 }
11086 
11087 //******************************************************************************
11088 // claimSystemBootEvent
11089 //
11090 // For a driver to claim a device is the source/conduit of a system boot event.
11091 //******************************************************************************
11092 
11093 void
11094 IOPMrootDomain::claimSystemBootEvent(
11095 	IOService *              device,
11096 	IOOptionBits             flags,
11097 	const char *             reason,
11098 	__unused OSObject *      details )
11099 {
11100 	if (!device || !reason) {
11101 		return;
11102 	}
11103 
11104 	DEBUG_LOG("claimSystemBootEvent(%s, %s, 0x%x)\n", reason, device->getName(), (uint32_t) flags);
11105 #if DEVELOPMENT || DEBUG
11106 	record_system_event(SYSTEM_EVENT_TYPE_INFO,
11107 	    SYSTEM_EVENT_SUBSYSTEM_PMRD,
11108 	    "Report System Boot Device",
11109 	    "Reason: %s Flags: 0x%x Device: %s",
11110 	    reason,
11111 	    (int)flags,
11112 	    device->getName()
11113 	    );
11114 #endif /* DEVELOPMENT || DEBUG */
11115 	WAKEEVENT_LOCK();
11116 	if (!gBootReasonSysctlRegistered) {
11117 		// Lazy sysctl registration after setting gBootReasonString
11118 		strlcat(gBootReasonString, reason, sizeof(gBootReasonString));
11119 		os_atomic_store(&gBootReasonSysctlRegistered, true, release);
11120 	}
11121 	WAKEEVENT_UNLOCK();
11122 }
11123 
11124 //******************************************************************************
11125 // claimSystemShutdownEvent
11126 //
11127 // For drivers to claim a system shutdown event on the ensuing boot.
11128 //******************************************************************************
11129 
11130 void
11131 IOPMrootDomain::claimSystemShutdownEvent(
11132 	IOService *              device,
11133 	IOOptionBits             flags,
11134 	const char *             reason,
11135 	__unused OSObject *      details )
11136 {
11137 	if (!device || !reason) {
11138 		return;
11139 	}
11140 
11141 	DEBUG_LOG("claimSystemShutdownEvent(%s, %s, 0x%x)\n", reason, device->getName(), (uint32_t) flags);
11142 #if DEVELOPMENT || DEBUG
11143 	record_system_event(SYSTEM_EVENT_TYPE_INFO,
11144 	    SYSTEM_EVENT_SUBSYSTEM_PMRD,
11145 	    "Report System Shutdown Cause From Previous Boot",
11146 	    "Reason: %s Flags: 0x%x Device: %s",
11147 	    reason,
11148 	    (int)flags,
11149 	    device->getName()
11150 	    );
11151 #endif /* DEVELOPMENT || DEBUG */
11152 	WAKEEVENT_LOCK();
11153 	if (gShutdownReasonString[0] != '\0') {
11154 		strlcat(gShutdownReasonString, " ", sizeof(gShutdownReasonString));
11155 	}
11156 	strlcat(gShutdownReasonString, reason, sizeof(gShutdownReasonString));
11157 
11158 	gShutdownReasonSysctlRegistered = true;
11159 	WAKEEVENT_UNLOCK();
11160 }
11161 
11162 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
11163 
11164 // MARK: -
11165 // MARK: PMSettingHandle
11166 
11167 OSDefineMetaClassAndStructors( PMSettingHandle, OSObject )
11168 
11169 void
11170 PMSettingHandle::free( void )
11171 {
11172 	if (pmso) {
11173 		pmso->clientHandleFreed();
11174 		pmso->release();
11175 		pmso = NULL;
11176 	}
11177 
11178 	OSObject::free();
11179 }
11180 
11181 // MARK: -
11182 // MARK: PMSettingObject
11183 
11184 #undef super
11185 #define super OSObject
11186 OSDefineMetaClassAndFinalStructors( PMSettingObject, OSObject )
11187 
11188 /*
11189  * Static constructor/initializer for PMSettingObject
11190  */
11191 PMSettingObject *PMSettingObject::pmSettingObject(
11192 	IOPMrootDomain                      * parent_arg,
11193 	IOPMSettingControllerCallback       handler_arg,
11194 	OSObject                            * target_arg,
11195 	uintptr_t                           refcon_arg,
11196 	uint32_t                            supportedPowerSources,
11197 	const OSSymbol *                    settings[],
11198 	OSObject                            * *handle_obj)
11199 {
11200 	uint32_t                            settingCount = 0;
11201 	PMSettingObject                     *pmso = NULL;
11202 	PMSettingHandle                     *pmsh = NULL;
11203 
11204 	if (!parent_arg || !handler_arg || !settings || !handle_obj) {
11205 		return NULL;
11206 	}
11207 
11208 	// count OSSymbol entries in NULL terminated settings array
11209 	while (settings[settingCount]) {
11210 		settingCount++;
11211 	}
11212 	if (0 == settingCount) {
11213 		return NULL;
11214 	}
11215 
11216 	pmso = new PMSettingObject;
11217 	if (!pmso || !pmso->init()) {
11218 		goto fail;
11219 	}
11220 
11221 	pmsh = new PMSettingHandle;
11222 	if (!pmsh || !pmsh->init()) {
11223 		goto fail;
11224 	}
11225 
11226 	queue_init(&pmso->calloutQueue);
11227 	pmso->parent       = parent_arg;
11228 	pmso->func         = handler_arg;
11229 	pmso->target       = target_arg;
11230 	pmso->refcon       = refcon_arg;
11231 	pmso->settingCount = settingCount;
11232 
11233 	pmso->retain(); // handle holds a retain on pmso
11234 	pmsh->pmso = pmso;
11235 	pmso->pmsh = pmsh;
11236 
11237 	pmso->publishedFeatureID = OSDataAllocation<uint32_t>(settingCount, OSAllocateMemory);
11238 	if (pmso->publishedFeatureID) {
11239 		for (unsigned int i = 0; i < settingCount; i++) {
11240 			// Since there is now at least one listener to this setting, publish
11241 			// PM root domain support for it.
11242 			parent_arg->publishPMSetting( settings[i],
11243 			    supportedPowerSources, &pmso->publishedFeatureID[i] );
11244 		}
11245 	}
11246 
11247 	*handle_obj = pmsh;
11248 	return pmso;
11249 
11250 fail:
11251 	if (pmso) {
11252 		pmso->release();
11253 	}
11254 	if (pmsh) {
11255 		pmsh->release();
11256 	}
11257 	return NULL;
11258 }
11259 
11260 void
11261 PMSettingObject::free( void )
11262 {
11263 	if (publishedFeatureID) {
11264 		for (const auto& featureID : publishedFeatureID) {
11265 			if (featureID) {
11266 				parent->removePublishedFeature( featureID );
11267 			}
11268 		}
11269 
11270 		publishedFeatureID = {};
11271 	}
11272 
11273 	super::free();
11274 }
11275 
11276 IOReturn
11277 PMSettingObject::dispatchPMSetting( const OSSymbol * type, OSObject * object )
11278 {
11279 	return (*func)(target, type, object, refcon);
11280 }
11281 
11282 void
11283 PMSettingObject::clientHandleFreed( void )
11284 {
11285 	parent->deregisterPMSettingObject(this);
11286 }
11287 
11288 // MARK: -
11289 // MARK: PMAssertionsTracker
11290 
11291 //*********************************************************************************
11292 //*********************************************************************************
11293 //*********************************************************************************
11294 // class PMAssertionsTracker Implementation
11295 
11296 #define kAssertUniqueIDStart    500
11297 
11298 PMAssertionsTracker *
11299 PMAssertionsTracker::pmAssertionsTracker( IOPMrootDomain *rootDomain )
11300 {
11301 	PMAssertionsTracker    *me;
11302 
11303 	me = new PMAssertionsTracker;
11304 	if (!me || !me->init()) {
11305 		if (me) {
11306 			me->release();
11307 		}
11308 		return NULL;
11309 	}
11310 
11311 	me->owner = rootDomain;
11312 	me->issuingUniqueID = kAssertUniqueIDStart;
11313 	me->assertionsArray = OSArray::withCapacity(5);
11314 	me->assertionsKernel = 0;
11315 	me->assertionsUser = 0;
11316 	me->assertionsCombined = 0;
11317 	me->assertionsArrayLock = IOLockAlloc();
11318 	me->tabulateProducerCount = me->tabulateConsumerCount = 0;
11319 
11320 	assert(me->assertionsArray);
11321 	assert(me->assertionsArrayLock);
11322 
11323 	return me;
11324 }
11325 
11326 /* tabulate
11327  * - Update assertionsKernel to reflect the state of all
11328  * assertions in the kernel.
11329  * - Update assertionsCombined to reflect both kernel & user space.
11330  */
11331 void
11332 PMAssertionsTracker::tabulate(void)
11333 {
11334 	int i;
11335 	int count;
11336 	const PMAssertStruct *_a = nullptr;
11337 	OSValueObject<PMAssertStruct> *_d = nullptr;
11338 
11339 	IOPMDriverAssertionType oldKernel = assertionsKernel;
11340 	IOPMDriverAssertionType oldCombined = assertionsCombined;
11341 
11342 	ASSERT_GATED();
11343 
11344 	assertionsKernel = 0;
11345 	assertionsCombined = 0;
11346 
11347 	if (!assertionsArray) {
11348 		return;
11349 	}
11350 
11351 	if ((count = assertionsArray->getCount())) {
11352 		for (i = 0; i < count; i++) {
11353 			_d = OSDynamicCast(OSValueObject<PMAssertStruct>, assertionsArray->getObject(i));
11354 			if (_d) {
11355 				_a = _d->getBytesNoCopy();
11356 				if (_a && (kIOPMDriverAssertionLevelOn == _a->level)) {
11357 					assertionsKernel |= _a->assertionBits;
11358 				}
11359 			}
11360 		}
11361 	}
11362 
11363 	tabulateProducerCount++;
11364 	assertionsCombined = assertionsKernel | assertionsUser;
11365 
11366 	if ((assertionsKernel != oldKernel) ||
11367 	    (assertionsCombined != oldCombined)) {
11368 		owner->evaluateAssertions(assertionsCombined, oldCombined);
11369 	}
11370 }
11371 
11372 void
11373 PMAssertionsTracker::updateCPUBitAccounting( PMAssertStruct *assertStruct )
11374 {
11375 	AbsoluteTime now;
11376 	uint64_t     nsec;
11377 
11378 	if (((assertStruct->assertionBits & kIOPMDriverAssertionCPUBit) == 0) ||
11379 	    (assertStruct->assertCPUStartTime == 0)) {
11380 		return;
11381 	}
11382 
11383 	now = mach_absolute_time();
11384 	SUB_ABSOLUTETIME(&now, &assertStruct->assertCPUStartTime);
11385 	absolutetime_to_nanoseconds(now, &nsec);
11386 	assertStruct->assertCPUDuration += nsec;
11387 	assertStruct->assertCPUStartTime = 0;
11388 
11389 	if (assertStruct->assertCPUDuration > maxAssertCPUDuration) {
11390 		maxAssertCPUDuration = assertStruct->assertCPUDuration;
11391 		maxAssertCPUEntryId = assertStruct->registryEntryID;
11392 	}
11393 }
11394 
11395 void
11396 PMAssertionsTracker::reportCPUBitAccounting( void )
11397 {
11398 	const PMAssertStruct *_a = nullptr;
11399 	OSValueObject<PMAssertStruct> *_d = nullptr;
11400 	int            i, count;
11401 	AbsoluteTime   now;
11402 	uint64_t       nsec;
11403 
11404 	ASSERT_GATED();
11405 
11406 	// Account for drivers that are still holding the CPU assertion
11407 	if (assertionsKernel & kIOPMDriverAssertionCPUBit) {
11408 		now = mach_absolute_time();
11409 		if ((count = assertionsArray->getCount())) {
11410 			for (i = 0; i < count; i++) {
11411 				_d = OSDynamicCast(OSValueObject<PMAssertStruct>, assertionsArray->getObject(i));
11412 				if (_d) {
11413 					_a = _d->getBytesNoCopy();
11414 					if ((_a->assertionBits & kIOPMDriverAssertionCPUBit) &&
11415 					    (_a->level == kIOPMDriverAssertionLevelOn) &&
11416 					    (_a->assertCPUStartTime != 0)) {
11417 						// Don't modify PMAssertStruct, leave that
11418 						// for updateCPUBitAccounting()
11419 						SUB_ABSOLUTETIME(&now, &_a->assertCPUStartTime);
11420 						absolutetime_to_nanoseconds(now, &nsec);
11421 						nsec += _a->assertCPUDuration;
11422 						if (nsec > maxAssertCPUDuration) {
11423 							maxAssertCPUDuration = nsec;
11424 							maxAssertCPUEntryId = _a->registryEntryID;
11425 						}
11426 					}
11427 				}
11428 			}
11429 		}
11430 	}
11431 
11432 	if (maxAssertCPUDuration) {
11433 		DLOG("cpu assertion held for %llu ms by 0x%llx\n",
11434 		    (maxAssertCPUDuration / NSEC_PER_MSEC), maxAssertCPUEntryId);
11435 	}
11436 
11437 	maxAssertCPUDuration = 0;
11438 	maxAssertCPUEntryId = 0;
11439 }
11440 
11441 void
11442 PMAssertionsTracker::publishProperties( void )
11443 {
11444 	OSSharedPtr<OSArray>             assertionsSummary;
11445 
11446 	if (tabulateConsumerCount != tabulateProducerCount) {
11447 		IOLockLock(assertionsArrayLock);
11448 
11449 		tabulateConsumerCount = tabulateProducerCount;
11450 
11451 		/* Publish the IOPMrootDomain property "DriverPMAssertionsDetailed"
11452 		 */
11453 		assertionsSummary = copyAssertionsArray();
11454 		if (assertionsSummary) {
11455 			owner->setProperty(kIOPMAssertionsDriverDetailedKey, assertionsSummary.get());
11456 		} else {
11457 			owner->removeProperty(kIOPMAssertionsDriverDetailedKey);
11458 		}
11459 
11460 		/* Publish the IOPMrootDomain property "DriverPMAssertions"
11461 		 */
11462 		owner->setProperty(kIOPMAssertionsDriverKey, assertionsKernel, 64);
11463 
11464 		IOLockUnlock(assertionsArrayLock);
11465 	}
11466 }
11467 
11468 PMAssertStruct *
11469 PMAssertionsTracker::detailsForID(IOPMDriverAssertionID _id, int *index)
11470 {
11471 	PMAssertStruct      *_a = NULL;
11472 	OSValueObject<PMAssertStruct> *_d = nullptr;
11473 	int                 found = -1;
11474 	int                 count = 0;
11475 	int                 i = 0;
11476 
11477 	if (assertionsArray
11478 	    && (count = assertionsArray->getCount())) {
11479 		for (i = 0; i < count; i++) {
11480 			_d = OSDynamicCast(OSValueObject<PMAssertStruct>, assertionsArray->getObject(i));
11481 			if (_d) {
11482 				_a = _d->getMutableBytesNoCopy();
11483 				if (_a && (_id == _a->id)) {
11484 					found = i;
11485 					break;
11486 				}
11487 			}
11488 		}
11489 	}
11490 
11491 	if (-1 == found) {
11492 		return NULL;
11493 	} else {
11494 		if (index) {
11495 			*index = found;
11496 		}
11497 		return _a;
11498 	}
11499 }
11500 
11501 /* PMAssertionsTracker::handleCreateAssertion
11502  * Perform assertion work on the PM workloop. Do not call directly.
11503  */
11504 IOReturn
11505 PMAssertionsTracker::handleCreateAssertion(OSValueObject<PMAssertStruct> *newAssertion)
11506 {
11507 	PMAssertStruct *assertStruct = nullptr;
11508 
11509 	ASSERT_GATED();
11510 
11511 	if (newAssertion) {
11512 		IOLockLock(assertionsArrayLock);
11513 		assertStruct = newAssertion->getMutableBytesNoCopy();
11514 		if ((assertStruct->assertionBits & kIOPMDriverAssertionCPUBit) &&
11515 		    (assertStruct->level == kIOPMDriverAssertionLevelOn)) {
11516 			assertStruct->assertCPUStartTime = mach_absolute_time();
11517 		}
11518 		assertionsArray->setObject(newAssertion);
11519 		IOLockUnlock(assertionsArrayLock);
11520 		newAssertion->release();
11521 
11522 		tabulate();
11523 	}
11524 	return kIOReturnSuccess;
11525 }
11526 
11527 /* PMAssertionsTracker::createAssertion
11528  * createAssertion allocates memory for a new PM assertion, and affects system behavior, if
11529  * appropiate.
11530  */
11531 IOReturn
11532 PMAssertionsTracker::createAssertion(
11533 	IOPMDriverAssertionType which,
11534 	IOPMDriverAssertionLevel level,
11535 	IOService *serviceID,
11536 	const char *whoItIs,
11537 	IOPMDriverAssertionID *outID)
11538 {
11539 	OSSharedPtr<OSValueObject<PMAssertStruct> > dataStore;
11540 	PMAssertStruct  track;
11541 
11542 	// Warning: trillions and trillions of created assertions may overflow the unique ID.
11543 	track.id = OSIncrementAtomic64((SInt64*) &issuingUniqueID);
11544 	track.level = level;
11545 	track.assertionBits = which;
11546 
11547 	// NB: ownerString is explicitly managed by PMAssertStruct
11548 	// it will be released in `handleReleaseAssertion' below
11549 	track.ownerString = whoItIs ? OSSymbol::withCString(whoItIs).detach():nullptr;
11550 	track.ownerService = serviceID;
11551 	track.registryEntryID = serviceID ? serviceID->getRegistryEntryID():0;
11552 	track.modifiedTime = 0;
11553 	pmEventTimeStamp(&track.createdTime);
11554 	track.assertCPUStartTime = 0;
11555 	track.assertCPUDuration = 0;
11556 
11557 	dataStore = OSValueObjectWithValue(track);
11558 	if (!dataStore) {
11559 		if (track.ownerString) {
11560 			track.ownerString->release();
11561 			track.ownerString = NULL;
11562 		}
11563 		return kIOReturnNoMemory;
11564 	}
11565 
11566 	*outID = track.id;
11567 
11568 	if (owner && owner->pmPowerStateQueue) {
11569 		// queue action is responsible for releasing dataStore
11570 		owner->pmPowerStateQueue->submitPowerEvent(kPowerEventAssertionCreate, (void *)dataStore.detach());
11571 	}
11572 
11573 	return kIOReturnSuccess;
11574 }
11575 
11576 /* PMAssertionsTracker::handleReleaseAssertion
11577  * Runs in PM workloop. Do not call directly.
11578  */
11579 IOReturn
11580 PMAssertionsTracker::handleReleaseAssertion(
11581 	IOPMDriverAssertionID _id)
11582 {
11583 	ASSERT_GATED();
11584 
11585 	int             index;
11586 	PMAssertStruct  *assertStruct = detailsForID(_id, &index);
11587 
11588 	if (!assertStruct) {
11589 		return kIOReturnNotFound;
11590 	}
11591 
11592 	IOLockLock(assertionsArrayLock);
11593 
11594 	if ((assertStruct->assertionBits & kIOPMDriverAssertionCPUBit) &&
11595 	    (assertStruct->level == kIOPMDriverAssertionLevelOn)) {
11596 		updateCPUBitAccounting(assertStruct);
11597 	}
11598 
11599 	if (assertStruct->ownerString) {
11600 		assertStruct->ownerString->release();
11601 		assertStruct->ownerString = NULL;
11602 	}
11603 
11604 	assertionsArray->removeObject(index);
11605 	IOLockUnlock(assertionsArrayLock);
11606 
11607 	tabulate();
11608 	return kIOReturnSuccess;
11609 }
11610 
11611 /* PMAssertionsTracker::releaseAssertion
11612  * Releases an assertion and affects system behavior if appropiate.
11613  * Actual work happens on PM workloop.
11614  */
11615 IOReturn
11616 PMAssertionsTracker::releaseAssertion(
11617 	IOPMDriverAssertionID _id)
11618 {
11619 	if (owner && owner->pmPowerStateQueue) {
11620 		owner->pmPowerStateQueue->submitPowerEvent(kPowerEventAssertionRelease, NULL, _id);
11621 	}
11622 	return kIOReturnSuccess;
11623 }
11624 
11625 /* PMAssertionsTracker::handleSetAssertionLevel
11626  * Runs in PM workloop. Do not call directly.
11627  */
11628 IOReturn
11629 PMAssertionsTracker::handleSetAssertionLevel(
11630 	IOPMDriverAssertionID    _id,
11631 	IOPMDriverAssertionLevel _level)
11632 {
11633 	PMAssertStruct      *assertStruct = detailsForID(_id, NULL);
11634 
11635 	ASSERT_GATED();
11636 
11637 	if (!assertStruct) {
11638 		return kIOReturnNotFound;
11639 	}
11640 
11641 	IOLockLock(assertionsArrayLock);
11642 	pmEventTimeStamp(&assertStruct->modifiedTime);
11643 	if ((assertStruct->assertionBits & kIOPMDriverAssertionCPUBit) &&
11644 	    (assertStruct->level != _level)) {
11645 		if (_level == kIOPMDriverAssertionLevelOn) {
11646 			assertStruct->assertCPUStartTime = mach_absolute_time();
11647 		} else {
11648 			updateCPUBitAccounting(assertStruct);
11649 		}
11650 	}
11651 	assertStruct->level = _level;
11652 	IOLockUnlock(assertionsArrayLock);
11653 
11654 	tabulate();
11655 	return kIOReturnSuccess;
11656 }
11657 
11658 /* PMAssertionsTracker::setAssertionLevel
11659  */
11660 IOReturn
11661 PMAssertionsTracker::setAssertionLevel(
11662 	IOPMDriverAssertionID    _id,
11663 	IOPMDriverAssertionLevel _level)
11664 {
11665 	if (owner && owner->pmPowerStateQueue) {
11666 		owner->pmPowerStateQueue->submitPowerEvent(kPowerEventAssertionSetLevel,
11667 		    (void *)(uintptr_t)_level, _id);
11668 	}
11669 
11670 	return kIOReturnSuccess;
11671 }
11672 
11673 IOReturn
11674 PMAssertionsTracker::handleSetUserAssertionLevels(void * arg0)
11675 {
11676 	IOPMDriverAssertionType new_user_levels = *(IOPMDriverAssertionType *) arg0;
11677 
11678 	ASSERT_GATED();
11679 
11680 	if (new_user_levels != assertionsUser) {
11681 		DLOG("assertionsUser 0x%llx->0x%llx\n", assertionsUser, new_user_levels);
11682 		assertionsUser = new_user_levels;
11683 	}
11684 
11685 	tabulate();
11686 	return kIOReturnSuccess;
11687 }
11688 
11689 IOReturn
11690 PMAssertionsTracker::setUserAssertionLevels(
11691 	IOPMDriverAssertionType new_user_levels)
11692 {
11693 	if (gIOPMWorkLoop) {
11694 		gIOPMWorkLoop->runAction(
11695 			OSMemberFunctionCast(
11696 				IOWorkLoop::Action,
11697 				this,
11698 				&PMAssertionsTracker::handleSetUserAssertionLevels),
11699 			this,
11700 			(void *) &new_user_levels, NULL, NULL, NULL);
11701 	}
11702 
11703 	return kIOReturnSuccess;
11704 }
11705 
11706 
11707 OSSharedPtr<OSArray>
11708 PMAssertionsTracker::copyAssertionsArray(void)
11709 {
11710 	int count;
11711 	int i;
11712 	OSSharedPtr<OSArray>     outArray = NULL;
11713 
11714 	if (!assertionsArray || (0 == (count = assertionsArray->getCount()))) {
11715 		goto exit;
11716 	}
11717 	outArray = OSArray::withCapacity(count);
11718 	if (!outArray) {
11719 		goto exit;
11720 	}
11721 
11722 	for (i = 0; i < count; i++) {
11723 		const PMAssertStruct *_a = nullptr;
11724 		OSValueObject<PMAssertStruct> *_d = nullptr;
11725 		OSSharedPtr<OSDictionary>    details;
11726 
11727 		_d = OSDynamicCast(OSValueObject<PMAssertStruct>, assertionsArray->getObject(i));
11728 		if (_d && (_a = _d->getBytesNoCopy())) {
11729 			OSSharedPtr<OSNumber>        _n;
11730 
11731 			details = OSDictionary::withCapacity(7);
11732 			if (!details) {
11733 				continue;
11734 			}
11735 
11736 			outArray->setObject(details.get());
11737 
11738 			_n = OSNumber::withNumber(_a->id, 64);
11739 			if (_n) {
11740 				details->setObject(kIOPMDriverAssertionIDKey, _n.get());
11741 			}
11742 			_n = OSNumber::withNumber(_a->createdTime, 64);
11743 			if (_n) {
11744 				details->setObject(kIOPMDriverAssertionCreatedTimeKey, _n.get());
11745 			}
11746 			_n = OSNumber::withNumber(_a->modifiedTime, 64);
11747 			if (_n) {
11748 				details->setObject(kIOPMDriverAssertionModifiedTimeKey, _n.get());
11749 			}
11750 			_n = OSNumber::withNumber((uintptr_t)_a->registryEntryID, 64);
11751 			if (_n) {
11752 				details->setObject(kIOPMDriverAssertionRegistryEntryIDKey, _n.get());
11753 			}
11754 			_n = OSNumber::withNumber(_a->level, 64);
11755 			if (_n) {
11756 				details->setObject(kIOPMDriverAssertionLevelKey, _n.get());
11757 			}
11758 			_n = OSNumber::withNumber(_a->assertionBits, 64);
11759 			if (_n) {
11760 				details->setObject(kIOPMDriverAssertionAssertedKey, _n.get());
11761 			}
11762 
11763 			if (_a->ownerString) {
11764 				details->setObject(kIOPMDriverAssertionOwnerStringKey, _a->ownerString);
11765 			}
11766 		}
11767 	}
11768 
11769 exit:
11770 	return os::move(outArray);
11771 }
11772 
11773 IOPMDriverAssertionType
11774 PMAssertionsTracker::getActivatedAssertions(void)
11775 {
11776 	return assertionsCombined;
11777 }
11778 
11779 IOPMDriverAssertionLevel
11780 PMAssertionsTracker::getAssertionLevel(
11781 	IOPMDriverAssertionType type)
11782 {
11783 	// FIXME: unused and also wrong
11784 	if (type && ((type & assertionsKernel) == assertionsKernel)) {
11785 		return kIOPMDriverAssertionLevelOn;
11786 	} else {
11787 		return kIOPMDriverAssertionLevelOff;
11788 	}
11789 }
11790 
11791 //*********************************************************************************
11792 //*********************************************************************************
11793 //*********************************************************************************
11794 
11795 
11796 static void
11797 pmEventTimeStamp(uint64_t *recordTS)
11798 {
11799 	clock_sec_t     tsec;
11800 	clock_usec_t    tusec;
11801 
11802 	if (!recordTS) {
11803 		return;
11804 	}
11805 
11806 	// We assume tsec fits into 32 bits; 32 bits holds enough
11807 	// seconds for 136 years since the epoch in 1970.
11808 	clock_get_calendar_microtime(&tsec, &tusec);
11809 
11810 
11811 	// Pack the sec & microsec calendar time into a uint64_t, for fun.
11812 	*recordTS = 0;
11813 	*recordTS |= (uint32_t)tusec;
11814 	*recordTS |= ((uint64_t)tsec << 32);
11815 
11816 	return;
11817 }
11818 
11819 // MARK: -
11820 // MARK: IORootParent
11821 
11822 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
11823 
11824 OSDefineMetaClassAndFinalStructors(IORootParent, IOService)
11825 
11826 // The reason that root domain needs a root parent is to facilitate demand
11827 // sleep, since a power change from the root parent cannot be vetoed.
11828 //
11829 // The above statement is no longer true since root domain now performs
11830 // demand sleep using overrides. But root parent remains to avoid changing
11831 // the power tree stacking. Root parent is parked at the max power state.
11832 
11833 
11834 static IOPMPowerState patriarchPowerStates[2] =
11835 {
11836 	{1, 0, ON_POWER, 0, 0, 0, 0, 0, 0, 0, 0, 0},
11837 	{1, 0, ON_POWER, 0, 0, 0, 0, 0, 0, 0, 0, 0},
11838 };
11839 
11840 void
11841 IORootParent::initialize( void )
11842 {
11843 
11844 	gIOPMPSExternalConnectedKey = OSSymbol::withCStringNoCopy(kIOPMPSExternalConnectedKey);
11845 	gIOPMPSExternalChargeCapableKey = OSSymbol::withCStringNoCopy(kIOPMPSExternalChargeCapableKey);
11846 	gIOPMPSBatteryInstalledKey = OSSymbol::withCStringNoCopy(kIOPMPSBatteryInstalledKey);
11847 	gIOPMPSIsChargingKey = OSSymbol::withCStringNoCopy(kIOPMPSIsChargingKey);
11848 	gIOPMPSAtWarnLevelKey = OSSymbol::withCStringNoCopy(kIOPMPSAtWarnLevelKey);
11849 	gIOPMPSAtCriticalLevelKey = OSSymbol::withCStringNoCopy(kIOPMPSAtCriticalLevelKey);
11850 	gIOPMPSCurrentCapacityKey = OSSymbol::withCStringNoCopy(kIOPMPSCurrentCapacityKey);
11851 	gIOPMPSMaxCapacityKey = OSSymbol::withCStringNoCopy(kIOPMPSMaxCapacityKey);
11852 	gIOPMPSDesignCapacityKey = OSSymbol::withCStringNoCopy(kIOPMPSDesignCapacityKey);
11853 	gIOPMPSTimeRemainingKey = OSSymbol::withCStringNoCopy(kIOPMPSTimeRemainingKey);
11854 	gIOPMPSAmperageKey = OSSymbol::withCStringNoCopy(kIOPMPSAmperageKey);
11855 	gIOPMPSVoltageKey = OSSymbol::withCStringNoCopy(kIOPMPSVoltageKey);
11856 	gIOPMPSCycleCountKey = OSSymbol::withCStringNoCopy(kIOPMPSCycleCountKey);
11857 	gIOPMPSMaxErrKey = OSSymbol::withCStringNoCopy(kIOPMPSMaxErrKey);
11858 	gIOPMPSAdapterInfoKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterInfoKey);
11859 	gIOPMPSLocationKey = OSSymbol::withCStringNoCopy(kIOPMPSLocationKey);
11860 	gIOPMPSErrorConditionKey = OSSymbol::withCStringNoCopy(kIOPMPSErrorConditionKey);
11861 	gIOPMPSManufacturerKey = OSSymbol::withCStringNoCopy(kIOPMPSManufacturerKey);
11862 	gIOPMPSManufactureDateKey = OSSymbol::withCStringNoCopy(kIOPMPSManufactureDateKey);
11863 	gIOPMPSModelKey = OSSymbol::withCStringNoCopy(kIOPMPSModelKey);
11864 	gIOPMPSSerialKey = OSSymbol::withCStringNoCopy(kIOPMPSSerialKey);
11865 	gIOPMPSLegacyBatteryInfoKey = OSSymbol::withCStringNoCopy(kIOPMPSLegacyBatteryInfoKey);
11866 	gIOPMPSBatteryHealthKey = OSSymbol::withCStringNoCopy(kIOPMPSBatteryHealthKey);
11867 	gIOPMPSHealthConfidenceKey = OSSymbol::withCStringNoCopy(kIOPMPSHealthConfidenceKey);
11868 	gIOPMPSCapacityEstimatedKey = OSSymbol::withCStringNoCopy(kIOPMPSCapacityEstimatedKey);
11869 	gIOPMPSBatteryChargeStatusKey = OSSymbol::withCStringNoCopy(kIOPMPSBatteryChargeStatusKey);
11870 	gIOPMPSBatteryTemperatureKey = OSSymbol::withCStringNoCopy(kIOPMPSBatteryTemperatureKey);
11871 	gIOPMPSAdapterDetailsKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsKey);
11872 	gIOPMPSChargerConfigurationKey = OSSymbol::withCStringNoCopy(kIOPMPSChargerConfigurationKey);
11873 	gIOPMPSAdapterDetailsIDKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsIDKey);
11874 	gIOPMPSAdapterDetailsWattsKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsWattsKey);
11875 	gIOPMPSAdapterDetailsRevisionKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsRevisionKey);
11876 	gIOPMPSAdapterDetailsSerialNumberKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsSerialNumberKey);
11877 	gIOPMPSAdapterDetailsFamilyKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsFamilyKey);
11878 	gIOPMPSAdapterDetailsAmperageKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsAmperageKey);
11879 	gIOPMPSAdapterDetailsDescriptionKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsDescriptionKey);
11880 	gIOPMPSAdapterDetailsPMUConfigurationKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsPMUConfigurationKey);
11881 	gIOPMPSAdapterDetailsSourceIDKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsSourceIDKey);
11882 	gIOPMPSAdapterDetailsErrorFlagsKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsErrorFlagsKey);
11883 	gIOPMPSAdapterDetailsSharedSourceKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsSharedSourceKey);
11884 	gIOPMPSAdapterDetailsCloakedKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsCloakedKey);
11885 	gIOPMPSInvalidWakeSecondsKey = OSSymbol::withCStringNoCopy(kIOPMPSInvalidWakeSecondsKey);
11886 	gIOPMPSPostChargeWaitSecondsKey = OSSymbol::withCStringNoCopy(kIOPMPSPostChargeWaitSecondsKey);
11887 	gIOPMPSPostDishargeWaitSecondsKey = OSSymbol::withCStringNoCopy(kIOPMPSPostDishargeWaitSecondsKey);
11888 }
11889 
11890 bool
11891 IORootParent::start( IOService * nub )
11892 {
11893 	IOService::start(nub);
11894 	attachToParent( getRegistryRoot(), gIOPowerPlane );
11895 	PMinit();
11896 	registerPowerDriver(this, patriarchPowerStates, 2);
11897 	makeUsable();
11898 	return true;
11899 }
11900 
11901 void
11902 IORootParent::shutDownSystem( void )
11903 {
11904 }
11905 
11906 void
11907 IORootParent::restartSystem( void )
11908 {
11909 }
11910 
11911 void
11912 IORootParent::sleepSystem( void )
11913 {
11914 }
11915 
11916 void
11917 IORootParent::dozeSystem( void )
11918 {
11919 }
11920 
11921 void
11922 IORootParent::sleepToDoze( void )
11923 {
11924 }
11925 
11926 void
11927 IORootParent::wakeSystem( void )
11928 {
11929 }
11930 
11931 OSSharedPtr<OSObject>
11932 IORootParent::copyProperty( const char * aKey) const
11933 {
11934 	return IOService::copyProperty(aKey);
11935 }
11936 
11937 uint32_t
11938 IOPMrootDomain::getWatchdogTimeout()
11939 {
11940 	if (gSwdSleepWakeTimeout) {
11941 		gSwdSleepTimeout = gSwdWakeTimeout = gSwdSleepWakeTimeout;
11942 	}
11943 	if ((pmTracer->getTracePhase() < kIOPMTracePointSystemSleep) ||
11944 	    (pmTracer->getTracePhase() == kIOPMTracePointDarkWakeEntry)) {
11945 		return gSwdSleepTimeout ? gSwdSleepTimeout : WATCHDOG_SLEEP_TIMEOUT;
11946 	} else {
11947 		return gSwdWakeTimeout ? gSwdWakeTimeout : WATCHDOG_WAKE_TIMEOUT;
11948 	}
11949 }
11950 
11951 static void
11952 reportAnalyticsTimerExpired(
11953 	thread_call_param_t us, thread_call_param_t )
11954 {
11955 	((IOPMrootDomain *)us)->reportAnalytics();
11956 }
11957 
11958 void
11959 IOPMrootDomain::initAOTMetrics()
11960 {
11961 	if (!_aotMetrics) {
11962 		_aotMetrics = IOMallocType(IOPMAOTMetrics);
11963 	}
11964 
11965 	if (!_aotAnalytics) {
11966 		_aotAnalytics = IOMallocType(IOPMAOTAnalytics);
11967 		analyticsThreadCall = thread_call_allocate_with_options(reportAnalyticsTimerExpired,
11968 		    (thread_call_param_t)this, THREAD_CALL_PRIORITY_KERNEL, THREAD_CALL_OPTIONS_ONCE);
11969 		scheduleAnalyticsThreadCall();
11970 	}
11971 }
11972 
11973 #define ANALYTICS_PERIOD_HOURS 24
11974 #define ANALYTICS_LEEWAY_MINUTES 5
11975 
11976 void
11977 IOPMrootDomain::scheduleAnalyticsThreadCall()
11978 {
11979 	uint64_t leeway, period, leeway_ns, period_ns, deadline;
11980 
11981 	period_ns = ANALYTICS_PERIOD_HOURS * 60 * 60 * NSEC_PER_SEC;
11982 	leeway_ns = ANALYTICS_LEEWAY_MINUTES * 60 * NSEC_PER_SEC;
11983 
11984 	nanoseconds_to_absolutetime(period_ns, &period);
11985 	nanoseconds_to_absolutetime(leeway_ns, &leeway);
11986 	deadline = mach_continuous_time() + period;
11987 	thread_call_enter_delayed_with_leeway(analyticsThreadCall, NULL, deadline, leeway, THREAD_CALL_CONTINUOUS);
11988 }
11989 
11990 CA_EVENT(aot_analytics,
11991     CA_INT, possible_count,
11992     CA_INT, confirmed_possible_count,
11993     CA_INT, rejected_possible_count,
11994     CA_INT, expired_possible_count);
11995 
11996 void
11997 IOPMrootDomain::reportAnalytics()
11998 {
11999 	if (_aotAnalytics) {
12000 		IOLog("IOPMAOTAnalytics possibleCount: %u, confirmedPossibleCount: %u, rejectedPossibleCount: %u, expiredPossibleCount: %u\n",
12001 		    _aotAnalytics->possibleCount, _aotAnalytics->confirmedPossibleCount, _aotAnalytics->rejectedPossibleCount, _aotAnalytics->expiredPossibleCount);
12002 
12003 		ca_event_t event = CA_EVENT_ALLOCATE(aot_analytics);
12004 		CA_EVENT_TYPE(aot_analytics) * e = (CA_EVENT_TYPE(aot_analytics) *)event->data;
12005 		e->possible_count = _aotAnalytics->possibleCount;
12006 		e->confirmed_possible_count = _aotAnalytics->confirmedPossibleCount;
12007 		e->rejected_possible_count = _aotAnalytics->rejectedPossibleCount;
12008 		e->expired_possible_count = _aotAnalytics->expiredPossibleCount;
12009 		CA_EVENT_SEND(event);
12010 
12011 		bzero(_aotAnalytics, sizeof(IOPMAOTAnalytics));
12012 		scheduleAnalyticsThreadCall();
12013 	}
12014 }
12015 
12016 #if defined(__i386__) || defined(__x86_64__) || (defined(__arm64__) && HIBERNATION)
12017 IOReturn
12018 IOPMrootDomain::restartWithStackshot()
12019 {
12020 	takeStackshot(true);
12021 
12022 	return kIOReturnSuccess;
12023 }
12024 
12025 void
12026 IOPMrootDomain::sleepWakeDebugTrig(bool wdogTrigger)
12027 {
12028 	takeStackshot(wdogTrigger);
12029 }
12030 
12031 void
12032 IOPMrootDomain::tracePhase2String(uint32_t tracePhase, const char **phaseString, const char **description)
12033 {
12034 	switch (tracePhase) {
12035 	case kIOPMTracePointSleepStarted:
12036 		*phaseString = "kIOPMTracePointSleepStarted";
12037 		*description = "starting sleep";
12038 		break;
12039 
12040 	case kIOPMTracePointSleepApplications:
12041 		*phaseString = "kIOPMTracePointSleepApplications";
12042 		*description = "notifying applications";
12043 		break;
12044 
12045 	case kIOPMTracePointSleepPriorityClients:
12046 		*phaseString = "kIOPMTracePointSleepPriorityClients";
12047 		*description = "notifying clients about upcoming system capability changes";
12048 		break;
12049 
12050 	case kIOPMTracePointSleepWillChangeInterests:
12051 		*phaseString = "kIOPMTracePointSleepWillChangeInterests";
12052 		*description = "creating hibernation file or while calling rootDomain's clients about upcoming rootDomain's state changes";
12053 		break;
12054 
12055 	case kIOPMTracePointSleepPowerPlaneDrivers:
12056 		*phaseString = "kIOPMTracePointSleepPowerPlaneDrivers";
12057 		*description = "calling power state change callbacks";
12058 		break;
12059 
12060 	case kIOPMTracePointSleepDidChangeInterests:
12061 		*phaseString = "kIOPMTracePointSleepDidChangeInterests";
12062 		*description = "calling rootDomain's clients about rootDomain's state changes";
12063 		break;
12064 
12065 	case kIOPMTracePointSleepCapabilityClients:
12066 		*phaseString = "kIOPMTracePointSleepCapabilityClients";
12067 		*description = "notifying clients about current system capabilities";
12068 		break;
12069 
12070 	case kIOPMTracePointSleepPlatformActions:
12071 		*phaseString = "kIOPMTracePointSleepPlatformActions";
12072 		*description = "calling Quiesce/Sleep action callbacks";
12073 		break;
12074 
12075 	case kIOPMTracePointSleepCPUs:
12076 	{
12077 		*phaseString = "kIOPMTracePointSleepCPUs";
12078 #if defined(__i386__) || defined(__x86_64__)
12079 		/*
12080 		 * We cannot use the getCPUNumber() method to get the cpu number, since
12081 		 * that cpu number is unrelated to the cpu number we need (we need the cpu
12082 		 * number as enumerated by the scheduler, NOT the CPU number enumerated
12083 		 * by ACPIPlatform as the CPUs are enumerated in MADT order).
12084 		 * Instead, pass the Mach processor pointer associated with the current
12085 		 * shutdown target so its associated cpu_id can be used in
12086 		 * processor_to_datastring.
12087 		 */
12088 		if (currentShutdownTarget != NULL &&
12089 		    currentShutdownTarget->getMachProcessor() != NULL) {
12090 			const char *sbuf = processor_to_datastring("halting all non-boot CPUs",
12091 			    currentShutdownTarget->getMachProcessor());
12092 			*description = sbuf;
12093 		} else {
12094 			*description = "halting all non-boot CPUs";
12095 		}
12096 #else
12097 		*description = "halting all non-boot CPUs";
12098 #endif
12099 		break;
12100 	}
12101 	case kIOPMTracePointSleepPlatformDriver:
12102 		*phaseString = "kIOPMTracePointSleepPlatformDriver";
12103 		*description = "executing platform specific code";
12104 		break;
12105 
12106 	case kIOPMTracePointHibernate:
12107 		*phaseString = "kIOPMTracePointHibernate";
12108 		*description = "writing the hibernation image";
12109 		break;
12110 
12111 	case kIOPMTracePointSystemSleep:
12112 		*phaseString = "kIOPMTracePointSystemSleep";
12113 		*description = "in EFI/Bootrom after last point of entry to sleep";
12114 		break;
12115 
12116 	case kIOPMTracePointWakePlatformDriver:
12117 		*phaseString = "kIOPMTracePointWakePlatformDriver";
12118 		*description = "executing platform specific code";
12119 		break;
12120 
12121 
12122 	case kIOPMTracePointWakePlatformActions:
12123 		*phaseString = "kIOPMTracePointWakePlatformActions";
12124 		*description = "calling Wake action callbacks";
12125 		break;
12126 
12127 	case kIOPMTracePointWakeCPUs:
12128 		*phaseString = "kIOPMTracePointWakeCPUs";
12129 		*description = "starting non-boot CPUs";
12130 		break;
12131 
12132 	case kIOPMTracePointWakeWillPowerOnClients:
12133 		*phaseString = "kIOPMTracePointWakeWillPowerOnClients";
12134 		*description = "sending kIOMessageSystemWillPowerOn message to kernel and userspace clients";
12135 		break;
12136 
12137 	case kIOPMTracePointWakeWillChangeInterests:
12138 		*phaseString = "kIOPMTracePointWakeWillChangeInterests";
12139 		*description = "calling rootDomain's clients about upcoming rootDomain's state changes";
12140 		break;
12141 
12142 	case kIOPMTracePointWakeDidChangeInterests:
12143 		*phaseString = "kIOPMTracePointWakeDidChangeInterests";
12144 		*description = "calling rootDomain's clients about completed rootDomain's state changes";
12145 		break;
12146 
12147 	case kIOPMTracePointWakePowerPlaneDrivers:
12148 		*phaseString = "kIOPMTracePointWakePowerPlaneDrivers";
12149 		*description = "calling power state change callbacks";
12150 		break;
12151 
12152 	case kIOPMTracePointWakeCapabilityClients:
12153 		*phaseString = "kIOPMTracePointWakeCapabilityClients";
12154 		*description = "informing clients about current system capabilities";
12155 		break;
12156 
12157 	case kIOPMTracePointWakeApplications:
12158 		*phaseString = "kIOPMTracePointWakeApplications";
12159 		*description = "sending asynchronous kIOMessageSystemHasPoweredOn message to userspace clients";
12160 		break;
12161 
12162 	case kIOPMTracePointDarkWakeEntry:
12163 		*phaseString = "kIOPMTracePointDarkWakeEntry";
12164 		*description = "entering darkwake on way to sleep";
12165 		break;
12166 
12167 	case kIOPMTracePointDarkWakeExit:
12168 		*phaseString = "kIOPMTracePointDarkWakeExit";
12169 		*description = "entering fullwake from darkwake";
12170 		break;
12171 
12172 	default:
12173 		*phaseString = NULL;
12174 		*description = NULL;
12175 	}
12176 }
12177 
12178 void
12179 IOPMrootDomain::saveFailureData2File()
12180 {
12181 	unsigned int len = 0;
12182 	char  failureStr[512];
12183 	errno_t error;
12184 	char *outbuf;
12185 	OSNumber *statusCode;
12186 	uint64_t pmStatusCode = 0;
12187 	uint32_t phaseData = 0;
12188 	uint32_t phaseDetail = 0;
12189 	bool efiFailure = false;
12190 
12191 	OSSharedPtr<OSObject> statusCodeProp = copyProperty(kIOPMSleepWakeFailureCodeKey);
12192 	statusCode = OSDynamicCast(OSNumber, statusCodeProp.get());
12193 	if (statusCode) {
12194 		pmStatusCode = statusCode->unsigned64BitValue();
12195 		phaseData = pmStatusCode & 0xFFFFFFFF;
12196 		phaseDetail = (pmStatusCode >> 32) & 0xFFFFFFFF;
12197 		if ((phaseData & 0xFF) == kIOPMTracePointSystemSleep) {
12198 			LOG("Sleep Wake failure in EFI\n");
12199 			efiFailure = true;
12200 			failureStr[0] = 0;
12201 			snprintf(failureStr, sizeof(failureStr), "Sleep Wake failure in EFI\n\nFailure code:: 0x%08x 0x%08x\n\nPlease IGNORE the below stackshot\n", phaseDetail, phaseData);
12202 			len = (typeof(len))strnlen(failureStr, sizeof(failureStr));
12203 		}
12204 	}
12205 
12206 	if (!efiFailure) {
12207 		if (PEReadNVRAMProperty(kIOSleepWakeFailurePanic, NULL, &len)) {
12208 			swd_flags |= SWD_BOOT_BY_SW_WDOG;
12209 			PERemoveNVRAMProperty(kIOSleepWakeFailurePanic);
12210 			// dump panic will handle saving nvram data
12211 			return;
12212 		}
12213 
12214 		/* Keeping this around for capturing data during power
12215 		 * button press */
12216 
12217 		if (!PEReadNVRAMProperty(kIOSleepWakeFailureString, NULL, &len)) {
12218 			DLOG("No sleep wake failure string\n");
12219 			return;
12220 		}
12221 		if (len == 0) {
12222 			DLOG("Ignoring zero byte SleepWake failure string\n");
12223 			goto exit;
12224 		}
12225 
12226 		// if PMStatus code is zero, delete stackshot and return
12227 		if (statusCode) {
12228 			if (((pmStatusCode & 0xFFFFFFFF) & 0xFF) == 0) {
12229 				// there was no sleep wake failure
12230 				// this can happen if delete stackshot was called
12231 				// before take stackshot completed. Let us delete any
12232 				// sleep wake failure data in nvram
12233 				DLOG("Deleting stackshot on successful wake\n");
12234 				deleteStackshot();
12235 				return;
12236 			}
12237 		}
12238 
12239 		if (len > sizeof(failureStr)) {
12240 			len = sizeof(failureStr);
12241 		}
12242 		failureStr[0] = 0;
12243 		PEReadNVRAMProperty(kIOSleepWakeFailureString, failureStr, &len);
12244 	}
12245 	if (failureStr[0] != 0) {
12246 		error = sleepWakeDebugSaveFile(kSleepWakeFailureStringFile, failureStr, len);
12247 		if (error) {
12248 			DLOG("Failed to save SleepWake failure string to file. error:%d\n", error);
12249 		} else {
12250 			DLOG("Saved SleepWake failure string to file.\n");
12251 		}
12252 	}
12253 
12254 	if (!OSCompareAndSwap(0, 1, &gRootDomain->swd_lock)) {
12255 		goto exit;
12256 	}
12257 
12258 	if (swd_buffer) {
12259 		unsigned int len = 0;
12260 		errno_t error;
12261 		char nvram_var_name_buffer[20];
12262 		unsigned int concat_len = 0;
12263 		swd_hdr      *hdr = NULL;
12264 
12265 
12266 		hdr = (swd_hdr *)swd_buffer;
12267 		outbuf = (char *)hdr + hdr->spindump_offset;
12268 		OSBoundedArrayRef<char> boundedOutBuf(outbuf, hdr->alloc_size - hdr->spindump_offset);
12269 
12270 		for (int i = 0; i < 8; i++) {
12271 			snprintf(nvram_var_name_buffer, sizeof(nvram_var_name_buffer), "%s%02d", SWD_STACKSHOT_VAR_PREFIX, i + 1);
12272 			if (!PEReadNVRAMProperty(nvram_var_name_buffer, NULL, &len)) {
12273 				LOG("No SleepWake blob to read beyond chunk %d\n", i);
12274 				break;
12275 			}
12276 			if (PEReadNVRAMProperty(nvram_var_name_buffer, boundedOutBuf.slice(concat_len, len).data(), &len) == FALSE) {
12277 				PERemoveNVRAMProperty(nvram_var_name_buffer);
12278 				LOG("Could not read the property :-(\n");
12279 				break;
12280 			}
12281 			PERemoveNVRAMProperty(nvram_var_name_buffer);
12282 			concat_len += len;
12283 		}
12284 		LOG("Concatenated length for the SWD blob %d\n", concat_len);
12285 
12286 		if (concat_len) {
12287 			error = sleepWakeDebugSaveFile(kSleepWakeStacksFilename, outbuf, concat_len);
12288 			if (error) {
12289 				LOG("Failed to save SleepWake zipped data to file. error:%d\n", error);
12290 			} else {
12291 				LOG("Saved SleepWake zipped data to file.\n");
12292 			}
12293 		} else {
12294 			// There is a sleep wake failure string but no stackshot
12295 			// Write a placeholder stacks file so that swd runs
12296 			snprintf(outbuf, 20, "%s", "No stackshot data\n");
12297 			error = sleepWakeDebugSaveFile(kSleepWakeStacksFilename, outbuf, 20);
12298 			if (error) {
12299 				LOG("Failed to save SleepWake zipped data to file. error:%d\n", error);
12300 			} else {
12301 				LOG("Saved SleepWake zipped data to file.\n");
12302 			}
12303 		}
12304 	} else {
12305 		LOG("No buffer allocated to save failure stackshot\n");
12306 	}
12307 
12308 
12309 	gRootDomain->swd_lock = 0;
12310 exit:
12311 	PERemoveNVRAMProperty(kIOSleepWakeFailureString);
12312 	return;
12313 }
12314 
12315 
12316 void
12317 IOPMrootDomain::getFailureData(thread_t *thread, char *failureStr, size_t strLen)
12318 {
12319 	OSSharedPtr<IORegistryIterator>    iter;
12320 	OSSharedPtr<const OSSymbol>        kextName = NULL;
12321 	IORegistryEntry *       entry;
12322 	IOService *             node;
12323 	bool                    nodeFound = false;
12324 
12325 	const void *            callMethod = NULL;
12326 	const char *            objectName = NULL;
12327 	uint32_t                timeout = getWatchdogTimeout();
12328 	const char *            phaseString = NULL;
12329 	const char *            phaseDescription = NULL;
12330 
12331 	IOPMServiceInterestNotifier *notifier = OSDynamicCast(IOPMServiceInterestNotifier, notifierObject.get());
12332 	uint32_t tracePhase = pmTracer->getTracePhase();
12333 
12334 	*thread = NULL;
12335 	if ((tracePhase < kIOPMTracePointSystemSleep) || (tracePhase == kIOPMTracePointDarkWakeEntry)) {
12336 		snprintf(failureStr, strLen, "Sleep transition timed out after %d seconds", timeout);
12337 	} else {
12338 		snprintf(failureStr, strLen, "Wake transition timed out after %d seconds", timeout);
12339 	}
12340 	tracePhase2String(tracePhase, &phaseString, &phaseDescription);
12341 
12342 	if (notifierThread) {
12343 		if (notifier && (notifier->identifier)) {
12344 			objectName = notifier->identifier->getCStringNoCopy();
12345 		}
12346 		*thread = notifierThread;
12347 	} else {
12348 		iter = IORegistryIterator::iterateOver(
12349 			getPMRootDomain(), gIOPowerPlane, kIORegistryIterateRecursively);
12350 
12351 		if (iter) {
12352 			while ((entry = iter->getNextObject())) {
12353 				node = OSDynamicCast(IOService, entry);
12354 				if (!node) {
12355 					continue;
12356 				}
12357 				if (OSDynamicCast(IOPowerConnection, node)) {
12358 					continue;
12359 				}
12360 
12361 				if (node->getBlockingDriverCall(thread, &callMethod)) {
12362 					nodeFound = true;
12363 					break;
12364 				}
12365 			}
12366 		}
12367 		if (nodeFound) {
12368 			kextName = copyKextIdentifierWithAddress((vm_address_t) callMethod);
12369 			if (kextName) {
12370 				objectName = kextName->getCStringNoCopy();
12371 			}
12372 		}
12373 	}
12374 	if (phaseDescription) {
12375 		strlcat(failureStr, " while ", strLen);
12376 		strlcat(failureStr, phaseDescription, strLen);
12377 		strlcat(failureStr, ".", strLen);
12378 	}
12379 	if (objectName) {
12380 		strlcat(failureStr, " Suspected bundle: ", strLen);
12381 		strlcat(failureStr, objectName, strLen);
12382 		strlcat(failureStr, ".", strLen);
12383 	}
12384 	if (*thread) {
12385 		char threadName[40];
12386 		snprintf(threadName, sizeof(threadName), " Thread 0x%llx.", thread_tid(*thread));
12387 		strlcat(failureStr, threadName, strLen);
12388 	}
12389 
12390 	DLOG("%s\n", failureStr);
12391 }
12392 
12393 struct swd_stackshot_compressed_data {
12394 	z_output_func   zoutput;
12395 	size_t                  zipped;
12396 	uint64_t                totalbytes;
12397 	uint64_t                lastpercent;
12398 	IOReturn                error;
12399 	unsigned                outremain;
12400 	unsigned                outlen;
12401 	unsigned                writes;
12402 	Bytef *                 outbuf;
12403 };
12404 struct swd_stackshot_compressed_data swd_zip_var = { };
12405 
12406 static void *
12407 swd_zs_alloc(void *__unused ref, u_int items, u_int size)
12408 {
12409 	void *result;
12410 	LOG("Alloc in zipping %d items of size %d\n", items, size);
12411 
12412 	result = (void *)(swd_zs_zmem + swd_zs_zoffset);
12413 	swd_zs_zoffset += ~31L & (31 + (items * size)); // 32b align for vector crc
12414 	LOG("Offset %zu\n", swd_zs_zoffset);
12415 	return result;
12416 }
12417 
12418 static int
12419 swd_zinput(z_streamp strm, Bytef *buf, unsigned size)
12420 {
12421 	unsigned len;
12422 
12423 	len = strm->avail_in;
12424 
12425 	if (len > size) {
12426 		len = size;
12427 	}
12428 	if (len == 0) {
12429 		return 0;
12430 	}
12431 
12432 	if (strm->next_in != (Bytef *) strm) {
12433 		memcpy(buf, strm->next_in, len);
12434 	} else {
12435 		bzero(buf, len);
12436 	}
12437 
12438 	strm->adler = z_crc32(strm->adler, buf, len);
12439 
12440 	strm->avail_in -= len;
12441 	strm->next_in  += len;
12442 	strm->total_in += len;
12443 
12444 	return (int)len;
12445 }
12446 
12447 static int
12448 swd_zoutput(z_streamp strm, Bytef *buf, unsigned len)
12449 {
12450 	unsigned int i = 0;
12451 	// if outlen > max size don't add to the buffer
12452 	assert(buf != NULL);
12453 	if (strm && buf) {
12454 		if (swd_zip_var.outlen + len > SWD_COMPRESSED_BUFSIZE) {
12455 			LOG("No space to GZIP... not writing to NVRAM\n");
12456 			return len;
12457 		}
12458 	}
12459 	for (i = 0; i < len; i++) {
12460 		*(swd_zip_var.outbuf + swd_zip_var.outlen + i) = *(buf + i);
12461 	}
12462 	swd_zip_var.outlen += len;
12463 	return len;
12464 }
12465 
12466 static void
12467 swd_zs_free(void * __unused ref, void * __unused ptr)
12468 {
12469 }
12470 
12471 static int
12472 swd_compress(char *inPtr, char *outPtr, size_t numBytes)
12473 {
12474 	int wbits = 12;
12475 	int memlevel = 3;
12476 
12477 	if (((unsigned int) numBytes) != numBytes) {
12478 		return 0;
12479 	}
12480 
12481 	if (!swd_zs.zalloc) {
12482 		swd_zs.zalloc = swd_zs_alloc;
12483 		swd_zs.zfree = swd_zs_free;
12484 		if (deflateInit2(&swd_zs, Z_BEST_SPEED, Z_DEFLATED, wbits + 16, memlevel, Z_DEFAULT_STRATEGY)) {
12485 			// allocation failed
12486 			bzero(&swd_zs, sizeof(swd_zs));
12487 			// swd_zs_zoffset = 0;
12488 		} else {
12489 			LOG("PMRD inited the zlib allocation routines\n");
12490 		}
12491 	}
12492 
12493 	swd_zip_var.zipped = 0;
12494 	swd_zip_var.totalbytes = 0; // should this be the max that we have?
12495 	swd_zip_var.lastpercent = 0;
12496 	swd_zip_var.error = kIOReturnSuccess;
12497 	swd_zip_var.outremain = 0;
12498 	swd_zip_var.outlen = 0;
12499 	swd_zip_var.writes = 0;
12500 	swd_zip_var.outbuf = (Bytef *)outPtr;
12501 
12502 	swd_zip_var.totalbytes = numBytes;
12503 
12504 	swd_zs.avail_in = 0;
12505 	swd_zs.next_in = NULL;
12506 	swd_zs.avail_out = 0;
12507 	swd_zs.next_out = NULL;
12508 
12509 	deflateResetWithIO(&swd_zs, swd_zinput, swd_zoutput);
12510 
12511 	z_stream *zs;
12512 	int zr;
12513 	zs = &swd_zs;
12514 
12515 	while (swd_zip_var.error >= 0) {
12516 		if (!zs->avail_in) {
12517 			zs->next_in = (unsigned char *)inPtr ? (Bytef *)inPtr : (Bytef *)zs; /* zero marker? */
12518 			zs->avail_in = (unsigned int) numBytes;
12519 		}
12520 		if (!zs->avail_out) {
12521 			zs->next_out = (Bytef *)zs;
12522 			zs->avail_out = UINT32_MAX;
12523 		}
12524 		zr = deflate(zs, Z_NO_FLUSH);
12525 		if (Z_STREAM_END == zr) {
12526 			break;
12527 		}
12528 		if (zr != Z_OK) {
12529 			LOG("ZERR %d\n", zr);
12530 			swd_zip_var.error = zr;
12531 		} else {
12532 			if (zs->total_in == numBytes) {
12533 				break;
12534 			}
12535 		}
12536 	}
12537 
12538 	//now flush the stream
12539 	while (swd_zip_var.error >= 0) {
12540 		if (!zs->avail_out) {
12541 			zs->next_out = (Bytef *)zs;
12542 			zs->avail_out = UINT32_MAX;
12543 		}
12544 		zr = deflate(zs, Z_FINISH);
12545 		if (Z_STREAM_END == zr) {
12546 			break;
12547 		}
12548 		if (zr != Z_OK) {
12549 			LOG("ZERR %d\n", zr);
12550 			swd_zip_var.error = zr;
12551 		} else {
12552 			if (zs->total_in == numBytes) {
12553 				LOG("Total output size %d\n", swd_zip_var.outlen);
12554 				break;
12555 			}
12556 		}
12557 	}
12558 
12559 	return swd_zip_var.outlen;
12560 }
12561 
12562 void
12563 IOPMrootDomain::deleteStackshot()
12564 {
12565 	if (!OSCompareAndSwap(0, 1, &gRootDomain->swd_lock)) {
12566 		// takeStackshot hasn't completed
12567 		return;
12568 	}
12569 	LOG("Deleting any sleepwake failure data in nvram\n");
12570 
12571 	PERemoveNVRAMProperty(kIOSleepWakeFailureString);
12572 	char nvram_var_name_buf[20];
12573 	for (int i = 0; i < 8; i++) {
12574 		snprintf(nvram_var_name_buf, sizeof(nvram_var_name_buf), "%s%02d", SWD_STACKSHOT_VAR_PREFIX, i + 1);
12575 		if (PERemoveNVRAMProperty(nvram_var_name_buf) == false) {
12576 			LOG("Removing %s returned false\n", nvram_var_name_buf);
12577 		}
12578 	}
12579 	// force NVRAM sync
12580 	if (PEWriteNVRAMProperty(kIONVRAMSyncNowPropertyKey, kIONVRAMSyncNowPropertyKey, (unsigned int) strlen(kIONVRAMSyncNowPropertyKey)) == false) {
12581 		DLOG("Failed to force nvram sync\n");
12582 	}
12583 	gRootDomain->swd_lock = 0;
12584 }
12585 
12586 void
12587 IOPMrootDomain::takeStackshot(bool wdogTrigger)
12588 {
12589 	swd_hdr *                hdr = NULL;
12590 	int                      cnt = 0;
12591 	int                      max_cnt;
12592 	pid_t                    pid = 0;
12593 	kern_return_t            kr = KERN_SUCCESS;
12594 	uint64_t                 flags;
12595 
12596 	char *                   dstAddr;
12597 	uint32_t                 size;
12598 	uint32_t                 bytesRemaining;
12599 	unsigned                 bytesWritten = 0;
12600 
12601 	char                     failureStr[512];
12602 	thread_t                 thread = NULL;
12603 	const char *             swfPanic = "swfPanic";
12604 
12605 	uint32_t                 bufSize;
12606 	int                      success = 0;
12607 
12608 #if defined(__i386__) || defined(__x86_64__)
12609 	const bool               concise = false;
12610 #else
12611 	const bool               concise = true;
12612 #endif
12613 
12614 	if (!OSCompareAndSwap(0, 1, &gRootDomain->swd_lock)) {
12615 		return;
12616 	}
12617 
12618 	failureStr[0] = 0;
12619 	if ((kIOSleepWakeWdogOff & gIOKitDebug) || systemBooting || systemShutdown || gWillShutdown) {
12620 		return;
12621 	}
12622 
12623 	if (wdogTrigger) {
12624 		getFailureData(&thread, failureStr, sizeof(failureStr));
12625 
12626 		if (concise || (PEGetCoprocessorVersion() >= kCoprocessorVersion2)) {
12627 			goto skip_stackshot;
12628 		}
12629 	} else {
12630 		AbsoluteTime now;
12631 		uint64_t nsec;
12632 		clock_get_uptime(&now);
12633 		SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
12634 		absolutetime_to_nanoseconds(now, &nsec);
12635 		snprintf(failureStr, sizeof(failureStr), "Power button pressed during wake transition after %u ms.\n", ((int)((nsec) / NSEC_PER_MSEC)));
12636 	}
12637 
12638 	if (swd_buffer == NULL) {
12639 		sleepWakeDebugMemAlloc();
12640 		if (swd_buffer == NULL) {
12641 			return;
12642 		}
12643 	}
12644 	hdr = (swd_hdr *)swd_buffer;
12645 	bufSize = hdr->alloc_size;
12646 
12647 	dstAddr = (char*)hdr + hdr->spindump_offset;
12648 	flags = STACKSHOT_KCDATA_FORMAT | STACKSHOT_NO_IO_STATS | STACKSHOT_SAVE_KEXT_LOADINFO | STACKSHOT_ACTIVE_KERNEL_THREADS_ONLY | STACKSHOT_THREAD_WAITINFO | STACKSHOT_INCLUDE_DRIVER_THREADS_IN_KERNEL;
12649 
12650 	/* If not wdogTrigger only take kernel tasks stackshot
12651 	 */
12652 	if (wdogTrigger) {
12653 		pid = -1;
12654 		max_cnt = 3;
12655 	} else {
12656 		pid = 0;
12657 		max_cnt = 2;
12658 	}
12659 
12660 	/* Attempt to take stackshot with all ACTIVE_KERNEL_THREADS
12661 	 * If we run out of space, take stackshot with only kernel task
12662 	 */
12663 	while (success == 0 && cnt < max_cnt) {
12664 		bytesRemaining = bufSize - hdr->spindump_offset;
12665 		cnt++;
12666 		DLOG("Taking snapshot. bytesRemaining: %d\n", bytesRemaining);
12667 
12668 		size = bytesRemaining;
12669 		kr = stack_snapshot_from_kernel(pid, dstAddr, size, flags, 0, 0, &bytesWritten);
12670 		DLOG("stack_snapshot_from_kernel returned 0x%x. pid: %d bufsize:0x%x flags:0x%llx bytesWritten: %d\n",
12671 		    kr, pid, size, flags, bytesWritten);
12672 		if (kr == KERN_INSUFFICIENT_BUFFER_SIZE) {
12673 			if (pid == -1) {
12674 				pid = 0;
12675 			} else if (flags & STACKSHOT_INCLUDE_DRIVER_THREADS_IN_KERNEL) {
12676 				flags = flags & ~STACKSHOT_INCLUDE_DRIVER_THREADS_IN_KERNEL;
12677 			} else {
12678 				LOG("Insufficient buffer size for only kernel task\n");
12679 				break;
12680 			}
12681 		}
12682 		if (kr == KERN_SUCCESS) {
12683 			if (bytesWritten == 0) {
12684 				MSG("Failed to get stackshot(0x%x) bufsize:0x%x flags:0x%llx\n", kr, size, flags);
12685 				continue;
12686 			}
12687 			bytesRemaining -= bytesWritten;
12688 			hdr->spindump_size = (bufSize - bytesRemaining - hdr->spindump_offset);
12689 
12690 			memset(hdr->reason, 0x20, sizeof(hdr->reason));
12691 
12692 			// Compress stackshot and save to NVRAM
12693 			{
12694 				char *outbuf = (char *)swd_compressed_buffer;
12695 				int outlen = 0;
12696 				int num_chunks = 0;
12697 				int max_chunks = 0;
12698 				int leftover = 0;
12699 				char nvram_var_name_buffer[20];
12700 
12701 				outlen = swd_compress((char*)hdr + hdr->spindump_offset, outbuf, bytesWritten);
12702 
12703 				if (outlen) {
12704 					max_chunks = outlen / (2096 - 200);
12705 					leftover = outlen % (2096 - 200);
12706 
12707 					if (max_chunks < 8) {
12708 						for (num_chunks = 0; num_chunks < max_chunks; num_chunks++) {
12709 							snprintf(nvram_var_name_buffer, sizeof(nvram_var_name_buffer), "%s%02d", SWD_STACKSHOT_VAR_PREFIX, num_chunks + 1);
12710 							if (PEWriteNVRAMPropertyWithCopy(nvram_var_name_buffer, (outbuf + (num_chunks * (2096 - 200))), (2096 - 200)) == FALSE) {
12711 								LOG("Failed to update NVRAM %d\n", num_chunks);
12712 								break;
12713 							}
12714 						}
12715 						if (leftover) {
12716 							snprintf(nvram_var_name_buffer, sizeof(nvram_var_name_buffer), "%s%02d", SWD_STACKSHOT_VAR_PREFIX, num_chunks + 1);
12717 							if (PEWriteNVRAMPropertyWithCopy(nvram_var_name_buffer, (outbuf + (num_chunks * (2096 - 200))), leftover) == FALSE) {
12718 								LOG("Failed to update NVRAM with leftovers\n");
12719 							}
12720 						}
12721 						success = 1;
12722 						LOG("Successfully saved stackshot to NVRAM\n");
12723 					} else {
12724 						if (pid == -1) {
12725 							LOG("Compressed failure stackshot is too large. size=%d bytes\n", outlen);
12726 							pid = 0;
12727 						} else if (flags & STACKSHOT_INCLUDE_DRIVER_THREADS_IN_KERNEL) {
12728 							LOG("Compressed failure stackshot of kernel+dexts is too large size=%d bytes\n", outlen);
12729 							flags = flags & ~STACKSHOT_INCLUDE_DRIVER_THREADS_IN_KERNEL;
12730 						} else {
12731 							LOG("Compressed failure stackshot of only kernel is too large size=%d bytes\n", outlen);
12732 							break;
12733 						}
12734 					}
12735 				}
12736 			}
12737 		}
12738 	}
12739 
12740 	if (failureStr[0]) {
12741 		// append sleep-wake failure code
12742 		char traceCode[80];
12743 		snprintf(traceCode, sizeof(traceCode), "\nFailure code:: 0x%08x %08x\n",
12744 		    pmTracer->getTraceData(), pmTracer->getTracePhase());
12745 		strlcat(failureStr, traceCode, sizeof(failureStr));
12746 		if (PEWriteNVRAMProperty(kIOSleepWakeFailureString, failureStr, (unsigned int) strnlen(failureStr, sizeof(failureStr))) == false) {
12747 			DLOG("Failed to write SleepWake failure string\n");
12748 		}
12749 	}
12750 
12751 	// force NVRAM sync
12752 	if (PEWriteNVRAMProperty(kIONVRAMSyncNowPropertyKey, kIONVRAMSyncNowPropertyKey, (unsigned int) strlen(kIONVRAMSyncNowPropertyKey)) == false) {
12753 		DLOG("Failed to force nvram sync\n");
12754 	}
12755 
12756 skip_stackshot:
12757 	if (wdogTrigger) {
12758 		if (PEGetCoprocessorVersion() < kCoprocessorVersion2) {
12759 			if (swd_flags & SWD_BOOT_BY_SW_WDOG) {
12760 				// If current boot is due to this watch dog trigger restart in previous boot,
12761 				// then don't trigger again until at least 1 successful sleep & wake.
12762 				if (!(sleepCnt && (displayWakeCnt || darkWakeCnt))) {
12763 					LOG("Shutting down due to repeated Sleep/Wake failures\n");
12764 					updateTasksSuspend(kTasksSuspendSuspended, kTasksSuspendNoChange);
12765 					PEHaltRestart(kPEHaltCPU);
12766 					return;
12767 				}
12768 			}
12769 			if (gSwdPanic == 0) {
12770 				LOG("Calling panic prevented by swd_panic boot-args. Calling restart");
12771 				updateTasksSuspend(kTasksSuspendSuspended, kTasksSuspendNoChange);
12772 				PEHaltRestart(kPERestartCPU);
12773 			}
12774 		}
12775 		if (!concise && (PEWriteNVRAMProperty(kIOSleepWakeFailurePanic, swfPanic, (unsigned int) strlen(swfPanic)) == false)) {
12776 			DLOG("Failed to write SleepWake failure panic key\n");
12777 		}
12778 #if defined(__x86_64__)
12779 		if (thread) {
12780 			panic_with_thread_context(0, NULL, DEBUGGER_OPTION_ATTEMPTCOREDUMPANDREBOOT, thread, "%s", failureStr);
12781 		} else
12782 #endif /* defined(__x86_64__) */
12783 		{
12784 			panic_with_options(0, NULL, DEBUGGER_OPTION_ATTEMPTCOREDUMPANDREBOOT, "%s", failureStr);
12785 		}
12786 	} else {
12787 		gRootDomain->swd_lock = 0;
12788 		return;
12789 	}
12790 }
12791 
12792 void
12793 IOPMrootDomain::sleepWakeDebugMemAlloc()
12794 {
12795 	vm_size_t    size = SWD_STACKSHOT_SIZE + SWD_COMPRESSED_BUFSIZE + SWD_ZLIB_BUFSIZE;
12796 
12797 	swd_hdr      *hdr = NULL;
12798 	void         *bufPtr = NULL;
12799 
12800 	OSSharedPtr<IOBufferMemoryDescriptor>  memDesc;
12801 
12802 
12803 	if (kIOSleepWakeWdogOff & gIOKitDebug) {
12804 		return;
12805 	}
12806 
12807 	if (!OSCompareAndSwap(0, 1, &gRootDomain->swd_lock)) {
12808 		return;
12809 	}
12810 
12811 	memDesc = IOBufferMemoryDescriptor::inTaskWithOptions(
12812 		kernel_task, kIODirectionIn | kIOMemoryMapperNone,
12813 		size);
12814 	if (memDesc == NULL) {
12815 		DLOG("Failed to allocate Memory descriptor for sleepWake debug\n");
12816 		goto exit;
12817 	}
12818 
12819 	bufPtr = memDesc->getBytesNoCopy();
12820 
12821 	// Carve out memory for zlib routines
12822 	swd_zs_zmem = (vm_offset_t)bufPtr;
12823 	bufPtr = (char *)bufPtr + SWD_ZLIB_BUFSIZE;
12824 
12825 	// Carve out memory for compressed stackshots
12826 	swd_compressed_buffer = bufPtr;
12827 	bufPtr = (char *)bufPtr + SWD_COMPRESSED_BUFSIZE;
12828 
12829 	// Remaining is used for holding stackshot
12830 	hdr = (swd_hdr *)bufPtr;
12831 	memset(hdr, 0, sizeof(swd_hdr));
12832 
12833 	hdr->signature = SWD_HDR_SIGNATURE;
12834 	hdr->alloc_size = SWD_STACKSHOT_SIZE;
12835 
12836 	hdr->spindump_offset = sizeof(swd_hdr);
12837 	swd_buffer = (void *)hdr;
12838 	swd_memDesc = os::move(memDesc);
12839 	DLOG("SleepWake debug buffer size:0x%x spindump offset:0x%x\n", hdr->alloc_size, hdr->spindump_offset);
12840 
12841 exit:
12842 	gRootDomain->swd_lock = 0;
12843 }
12844 
12845 void
12846 IOPMrootDomain::sleepWakeDebugSpinDumpMemAlloc()
12847 {
12848 #if UNUSED
12849 	vm_size_t    size = SWD_SPINDUMP_SIZE;
12850 
12851 	swd_hdr      *hdr = NULL;
12852 
12853 	OSSharedPtr<IOBufferMemoryDescriptor>  memDesc;
12854 
12855 	if (!OSCompareAndSwap(0, 1, &gRootDomain->swd_lock)) {
12856 		return;
12857 	}
12858 
12859 	memDesc = IOBufferMemoryDescriptor::inTaskWithOptions(
12860 		kernel_task, kIODirectionIn | kIOMemoryMapperNone,
12861 		SWD_SPINDUMP_SIZE);
12862 
12863 	if (memDesc == NULL) {
12864 		DLOG("Failed to allocate Memory descriptor for sleepWake debug spindump\n");
12865 		goto exit;
12866 	}
12867 
12868 
12869 	hdr = (swd_hdr *)memDesc->getBytesNoCopy();
12870 	memset(hdr, 0, sizeof(swd_hdr));
12871 
12872 	hdr->signature = SWD_HDR_SIGNATURE;
12873 	hdr->alloc_size = size;
12874 
12875 	hdr->spindump_offset = sizeof(swd_hdr);
12876 	swd_spindump_buffer = (void *)hdr;
12877 	swd_spindump_memDesc = os::move(memDesc);
12878 
12879 exit:
12880 	gRootDomain->swd_lock = 0;
12881 #endif /* UNUSED */
12882 }
12883 
12884 void
12885 IOPMrootDomain::sleepWakeDebugEnableWdog()
12886 {
12887 }
12888 
12889 bool
12890 IOPMrootDomain::sleepWakeDebugIsWdogEnabled()
12891 {
12892 	return !systemBooting && !systemShutdown && !gWillShutdown;
12893 }
12894 
12895 void
12896 IOPMrootDomain::sleepWakeDebugSaveSpinDumpFile()
12897 {
12898 	swd_hdr *hdr = NULL;
12899 	errno_t error = EIO;
12900 
12901 	if (swd_spindump_buffer && gSpinDumpBufferFull) {
12902 		hdr = (swd_hdr *)swd_spindump_buffer;
12903 
12904 		error = sleepWakeDebugSaveFile("/var/tmp/SleepWakeDelayStacks.dump",
12905 		    (char*)hdr + hdr->spindump_offset, hdr->spindump_size);
12906 
12907 		if (error) {
12908 			return;
12909 		}
12910 
12911 		sleepWakeDebugSaveFile("/var/tmp/SleepWakeDelayLog.dump",
12912 		    (char*)hdr + offsetof(swd_hdr, UUID),
12913 		    sizeof(swd_hdr) - offsetof(swd_hdr, UUID));
12914 
12915 		gSpinDumpBufferFull = false;
12916 	}
12917 }
12918 
12919 errno_t
12920 IOPMrootDomain::sleepWakeDebugSaveFile(const char *name, char *buf, int len)
12921 {
12922 	struct vnode         *vp = NULL;
12923 	vfs_context_t        ctx = vfs_context_create(vfs_context_current());
12924 	kauth_cred_t         cred = vfs_context_ucred(ctx);
12925 	struct vnode_attr    va;
12926 	errno_t      error = EIO;
12927 
12928 	if (vnode_open(name, (O_CREAT | FWRITE | O_NOFOLLOW),
12929 	    S_IRUSR | S_IRGRP | S_IROTH, VNODE_LOOKUP_NOFOLLOW, &vp, ctx) != 0) {
12930 		LOG("Failed to open the file %s\n", name);
12931 		swd_flags |= SWD_FILEOP_ERROR;
12932 		goto exit;
12933 	}
12934 	VATTR_INIT(&va);
12935 	VATTR_WANTED(&va, va_nlink);
12936 	/* Don't dump to non-regular files or files with links. */
12937 	if (vp->v_type != VREG ||
12938 	    vnode_getattr(vp, &va, ctx) || va.va_nlink != 1) {
12939 		LOG("Bailing as this is not a regular file\n");
12940 		swd_flags |= SWD_FILEOP_ERROR;
12941 		goto exit;
12942 	}
12943 	VATTR_INIT(&va);
12944 	VATTR_SET(&va, va_data_size, 0);
12945 	vnode_setattr(vp, &va, ctx);
12946 
12947 
12948 	if (buf != NULL) {
12949 		error = vn_rdwr(UIO_WRITE, vp, buf, len, 0,
12950 		    UIO_SYSSPACE, IO_NODELOCKED | IO_UNIT, cred, (int *) NULL, vfs_context_proc(ctx));
12951 		if (error != 0) {
12952 			LOG("Failed to save sleep wake log. err 0x%x\n", error);
12953 			swd_flags |= SWD_FILEOP_ERROR;
12954 		} else {
12955 			DLOG("Saved %d bytes to file %s\n", len, name);
12956 		}
12957 	}
12958 
12959 exit:
12960 	if (vp) {
12961 		vnode_close(vp, FWRITE, ctx);
12962 	}
12963 	if (ctx) {
12964 		vfs_context_rele(ctx);
12965 	}
12966 
12967 	return error;
12968 }
12969 
12970 #else /* defined(__i386__) || defined(__x86_64__) */
12971 
12972 void
12973 IOPMrootDomain::sleepWakeDebugTrig(bool restart)
12974 {
12975 	if (restart) {
12976 		if (gSwdPanic == 0) {
12977 			return;
12978 		}
12979 		panic("Sleep/Wake hang detected");
12980 		return;
12981 	}
12982 }
12983 
12984 void
12985 IOPMrootDomain::takeStackshot(bool restart)
12986 {
12987 #pragma unused(restart)
12988 }
12989 
12990 void
12991 IOPMrootDomain::deleteStackshot()
12992 {
12993 }
12994 
12995 void
12996 IOPMrootDomain::sleepWakeDebugMemAlloc()
12997 {
12998 }
12999 
13000 void
13001 IOPMrootDomain::saveFailureData2File()
13002 {
13003 }
13004 
13005 void
13006 IOPMrootDomain::sleepWakeDebugEnableWdog()
13007 {
13008 }
13009 
13010 bool
13011 IOPMrootDomain::sleepWakeDebugIsWdogEnabled()
13012 {
13013 	return false;
13014 }
13015 
13016 void
13017 IOPMrootDomain::sleepWakeDebugSaveSpinDumpFile()
13018 {
13019 }
13020 
13021 errno_t
13022 IOPMrootDomain::sleepWakeDebugSaveFile(const char *name, char *buf, int len)
13023 {
13024 	return 0;
13025 }
13026 
13027 #endif /* defined(__i386__) || defined(__x86_64__) */
13028 
13029