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 <console/video_console.h>
64 #include <sys/syslog.h>
65 #include <sys/sysctl.h>
66 #include <sys/vnode.h>
67 #include <sys/vnode_internal.h>
68 #include <sys/fcntl.h>
69 #include <os/log.h>
70 #include <pexpert/protos.h>
71 #include <AssertMacros.h>
72
73 #include <sys/time.h>
74 #include "IOServicePrivate.h" // _IOServiceInterestNotifier
75 #include "IOServicePMPrivate.h"
76
77 #include <libkern/zlib.h>
78 #include <os/cpp_util.h>
79 #include <os/atomic_private.h>
80 #include <libkern/c++/OSBoundedArrayRef.h>
81
82 __BEGIN_DECLS
83 #include <mach/shared_region.h>
84 #include <kern/clock.h>
85 __END_DECLS
86
87 #if defined(__i386__) || defined(__x86_64__)
88 __BEGIN_DECLS
89 #include "IOPMrootDomainInternal.h"
90 const char *processor_to_datastring(const char *prefix, processor_t target_processor);
91 __END_DECLS
92 #endif
93
94 #define kIOPMrootDomainClass "IOPMrootDomain"
95 #define LOG_PREFIX "PMRD: "
96
97
98 #define MSG(x...) \
99 do { kprintf(LOG_PREFIX x); IOLog(x); } while (false)
100
101 #define LOG(x...) \
102 do { kprintf(LOG_PREFIX x); } while (false)
103
104 #if DEVELOPMENT || DEBUG
105 #define DEBUG_LOG(x...) do { \
106 if (kIOLogPMRootDomain & gIOKitDebug) \
107 kprintf(LOG_PREFIX x); \
108 os_log_debug(OS_LOG_DEFAULT, LOG_PREFIX x); \
109 } while (false)
110 #else
111 #define DEBUG_LOG(x...)
112 #endif
113
114 #define DLOG(x...) do { \
115 if (kIOLogPMRootDomain & gIOKitDebug) \
116 kprintf(LOG_PREFIX x); \
117 else \
118 os_log(OS_LOG_DEFAULT, LOG_PREFIX x); \
119 } while (false)
120
121 #define DMSG(x...) do { \
122 if (kIOLogPMRootDomain & gIOKitDebug) { \
123 kprintf(LOG_PREFIX x); \
124 } \
125 } while (false)
126
127
128 #define _LOG(x...)
129
130 #define CHECK_THREAD_CONTEXT
131 #ifdef CHECK_THREAD_CONTEXT
132 static IOWorkLoop * gIOPMWorkLoop = NULL;
133 #define ASSERT_GATED() \
134 do { \
135 if (gIOPMWorkLoop && gIOPMWorkLoop->inGate() != true) { \
136 panic("RootDomain: not inside PM gate"); \
137 } \
138 } while(false)
139 #else
140 #define ASSERT_GATED()
141 #endif /* CHECK_THREAD_CONTEXT */
142
143 #define CAP_LOSS(c) \
144 (((_pendingCapability & (c)) == 0) && \
145 ((_currentCapability & (c)) != 0))
146
147 #define CAP_GAIN(c) \
148 (((_currentCapability & (c)) == 0) && \
149 ((_pendingCapability & (c)) != 0))
150
151 #define CAP_CHANGE(c) \
152 (((_currentCapability ^ _pendingCapability) & (c)) != 0)
153
154 #define CAP_CURRENT(c) \
155 ((_currentCapability & (c)) != 0)
156
157 #define CAP_HIGHEST(c) \
158 ((_highestCapability & (c)) != 0)
159
160 #define CAP_PENDING(c) \
161 ((_pendingCapability & (c)) != 0)
162
163 // rdar://problem/9157444
164 #if defined(__i386__) || defined(__x86_64__)
165 #define DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY 20
166 #endif
167
168 // Event types for IOPMPowerStateQueue::submitPowerEvent()
169 enum {
170 kPowerEventFeatureChanged = 1, // 1
171 kPowerEventReceivedPowerNotification, // 2
172 kPowerEventSystemBootCompleted, // 3
173 kPowerEventSystemShutdown, // 4
174 kPowerEventUserDisabledSleep, // 5
175 kPowerEventRegisterSystemCapabilityClient, // 6
176 kPowerEventRegisterKernelCapabilityClient, // 7
177 kPowerEventPolicyStimulus, // 8
178 kPowerEventAssertionCreate, // 9
179 kPowerEventAssertionRelease, // 10
180 kPowerEventAssertionSetLevel, // 11
181 kPowerEventQueueSleepWakeUUID, // 12
182 kPowerEventPublishSleepWakeUUID, // 13
183 kPowerEventSetDisplayPowerOn, // 14
184 kPowerEventPublishWakeType, // 15
185 kPowerEventAOTEvaluate // 16
186 };
187
188 // For evaluatePolicy()
189 // List of stimuli that affects the root domain policy.
190 enum {
191 kStimulusDisplayWranglerSleep, // 0
192 kStimulusDisplayWranglerWake, // 1
193 kStimulusAggressivenessChanged, // 2
194 kStimulusDemandSystemSleep, // 3
195 kStimulusAllowSystemSleepChanged, // 4
196 kStimulusDarkWakeActivityTickle, // 5
197 kStimulusDarkWakeEntry, // 6
198 kStimulusDarkWakeReentry, // 7
199 kStimulusDarkWakeEvaluate, // 8
200 kStimulusNoIdleSleepPreventers, // 9
201 kStimulusEnterUserActiveState, // 10
202 kStimulusLeaveUserActiveState // 11
203 };
204
205 // Internal power state change reasons
206 // Must be less than kIOPMSleepReasonClamshell=101
207 enum {
208 kCPSReasonNone = 0, // 0
209 kCPSReasonInit, // 1
210 kCPSReasonWake, // 2
211 kCPSReasonIdleSleepPrevent, // 3
212 kCPSReasonIdleSleepAllow, // 4
213 kCPSReasonPowerOverride, // 5
214 kCPSReasonPowerDownCancel, // 6
215 kCPSReasonAOTExit, // 7
216 kCPSReasonAdjustPowerState, // 8
217 kCPSReasonDarkWakeCannotSleep, // 9
218 kCPSReasonIdleSleepEnabled, // 10
219 kCPSReasonEvaluatePolicy, // 11
220 kCPSReasonSustainFullWake, // 12
221 kCPSReasonPMInternals = (kIOPMSleepReasonClamshell - 1)
222 };
223
224 extern "C" {
225 IOReturn OSKextSystemSleepOrWake( UInt32 );
226 }
227 extern "C" ppnum_t pmap_find_phys(pmap_t pmap, addr64_t va);
228 extern "C" addr64_t kvtophys(vm_offset_t va);
229 extern "C" boolean_t kdp_has_polled_corefile();
230
231 static void idleSleepTimerExpired( thread_call_param_t, thread_call_param_t );
232 static void notifySystemShutdown( IOService * root, uint32_t messageType );
233 static void handleAggressivesFunction( thread_call_param_t, thread_call_param_t );
234 static void pmEventTimeStamp(uint64_t *recordTS);
235 static void powerButtonUpCallout( thread_call_param_t, thread_call_param_t );
236 static void powerButtonDownCallout( thread_call_param_t, thread_call_param_t );
237 static OSPtr<const OSSymbol> copyKextIdentifierWithAddress(vm_address_t address);
238
239 static int IOPMConvertSecondsToCalendar(clock_sec_t secs, IOPMCalendarStruct * dt);
240 static clock_sec_t IOPMConvertCalendarToSeconds(const IOPMCalendarStruct * dt);
241 #define YMDTF "%04d/%02d/%d %02d:%02d:%02d"
242 #define YMDT(cal) ((int)(cal)->year), (cal)->month, (cal)->day, (cal)->hour, (cal)->minute, (cal)->second
243
244 // "IOPMSetSleepSupported" callPlatformFunction name
245 static OSSharedPtr<const OSSymbol> sleepSupportedPEFunction;
246 static OSSharedPtr<const OSSymbol> sleepMessagePEFunction;
247 static OSSharedPtr<const OSSymbol> gIOPMWakeTypeUserKey;
248
249 static OSSharedPtr<const OSSymbol> gIOPMPSExternalConnectedKey;
250 static OSSharedPtr<const OSSymbol> gIOPMPSExternalChargeCapableKey;
251 static OSSharedPtr<const OSSymbol> gIOPMPSBatteryInstalledKey;
252 static OSSharedPtr<const OSSymbol> gIOPMPSIsChargingKey;
253 static OSSharedPtr<const OSSymbol> gIOPMPSAtWarnLevelKey;
254 static OSSharedPtr<const OSSymbol> gIOPMPSAtCriticalLevelKey;
255 static OSSharedPtr<const OSSymbol> gIOPMPSCurrentCapacityKey;
256 static OSSharedPtr<const OSSymbol> gIOPMPSMaxCapacityKey;
257 static OSSharedPtr<const OSSymbol> gIOPMPSDesignCapacityKey;
258 static OSSharedPtr<const OSSymbol> gIOPMPSTimeRemainingKey;
259 static OSSharedPtr<const OSSymbol> gIOPMPSAmperageKey;
260 static OSSharedPtr<const OSSymbol> gIOPMPSVoltageKey;
261 static OSSharedPtr<const OSSymbol> gIOPMPSCycleCountKey;
262 static OSSharedPtr<const OSSymbol> gIOPMPSMaxErrKey;
263 static OSSharedPtr<const OSSymbol> gIOPMPSAdapterInfoKey;
264 static OSSharedPtr<const OSSymbol> gIOPMPSLocationKey;
265 static OSSharedPtr<const OSSymbol> gIOPMPSErrorConditionKey;
266 static OSSharedPtr<const OSSymbol> gIOPMPSManufacturerKey;
267 static OSSharedPtr<const OSSymbol> gIOPMPSManufactureDateKey;
268 static OSSharedPtr<const OSSymbol> gIOPMPSModelKey;
269 static OSSharedPtr<const OSSymbol> gIOPMPSSerialKey;
270 static OSSharedPtr<const OSSymbol> gIOPMPSLegacyBatteryInfoKey;
271 static OSSharedPtr<const OSSymbol> gIOPMPSBatteryHealthKey;
272 static OSSharedPtr<const OSSymbol> gIOPMPSHealthConfidenceKey;
273 static OSSharedPtr<const OSSymbol> gIOPMPSCapacityEstimatedKey;
274 static OSSharedPtr<const OSSymbol> gIOPMPSBatteryChargeStatusKey;
275 static OSSharedPtr<const OSSymbol> gIOPMPSBatteryTemperatureKey;
276 static OSSharedPtr<const OSSymbol> gIOPMPSAdapterDetailsKey;
277 static OSSharedPtr<const OSSymbol> gIOPMPSChargerConfigurationKey;
278 static OSSharedPtr<const OSSymbol> gIOPMPSAdapterDetailsIDKey;
279 static OSSharedPtr<const OSSymbol> gIOPMPSAdapterDetailsWattsKey;
280 static OSSharedPtr<const OSSymbol> gIOPMPSAdapterDetailsRevisionKey;
281 static OSSharedPtr<const OSSymbol> gIOPMPSAdapterDetailsSerialNumberKey;
282 static OSSharedPtr<const OSSymbol> gIOPMPSAdapterDetailsFamilyKey;
283 static OSSharedPtr<const OSSymbol> gIOPMPSAdapterDetailsAmperageKey;
284 static OSSharedPtr<const OSSymbol> gIOPMPSAdapterDetailsDescriptionKey;
285 static OSSharedPtr<const OSSymbol> gIOPMPSAdapterDetailsPMUConfigurationKey;
286 static OSSharedPtr<const OSSymbol> gIOPMPSAdapterDetailsSourceIDKey;
287 static OSSharedPtr<const OSSymbol> gIOPMPSAdapterDetailsErrorFlagsKey;
288 static OSSharedPtr<const OSSymbol> gIOPMPSAdapterDetailsSharedSourceKey;
289 static OSSharedPtr<const OSSymbol> gIOPMPSAdapterDetailsCloakedKey;
290 static OSSharedPtr<const OSSymbol> gIOPMPSInvalidWakeSecondsKey;
291 static OSSharedPtr<const OSSymbol> gIOPMPSPostChargeWaitSecondsKey;
292 static OSSharedPtr<const OSSymbol> gIOPMPSPostDishargeWaitSecondsKey;
293
294 #define kIOSleepSupportedKey "IOSleepSupported"
295 #define kIOPMSystemCapabilitiesKey "System Capabilities"
296 #define kIOPMSystemDefaultOverrideKey "SystemPowerProfileOverrideDict"
297
298 #define kIORequestWranglerIdleKey "IORequestIdle"
299 #define kDefaultWranglerIdlePeriod 1000 // in milliseconds
300
301 #define kIOSleepWakeFailureString "SleepWakeFailureString"
302 #define kIOEFIBootRomFailureKey "wake-failure"
303 #define kIOSleepWakeFailurePanic "SleepWakeFailurePanic"
304
305 #define kRD_AllPowerSources (kIOPMSupportedOnAC \
306 | kIOPMSupportedOnBatt \
307 | kIOPMSupportedOnUPS)
308
309 #define kLocalEvalClamshellCommand (1 << 15)
310 #define kIdleSleepRetryInterval (3 * 60 * 1000)
311
312 // Minimum time in milliseconds after AP wake that we allow idle timer to expire.
313 // We impose this minimum to avoid race conditions in the AP wake path where
314 // userspace clients are not able to acquire power assertions before the idle timer expires.
315 #define kMinimumTimeBeforeIdleSleep 1000
316
317 #define DISPLAY_WRANGLER_PRESENT (!NO_KERNEL_HID)
318
319 enum {
320 kWranglerPowerStateMin = 0,
321 kWranglerPowerStateSleep = 2,
322 kWranglerPowerStateDim = 3,
323 kWranglerPowerStateMax = 4
324 };
325
326 enum {
327 OFF_STATE = 0,
328 RESTART_STATE = 1,
329 SLEEP_STATE = 2,
330 AOT_STATE = 3,
331 ON_STATE = 4,
332 NUM_POWER_STATES
333 };
334
335 const char *
getPowerStateString(uint32_t state)336 getPowerStateString( uint32_t state )
337 {
338 #define POWER_STATE(x) {(uint32_t) x, #x}
339
340 static const IONamedValue powerStates[] = {
341 POWER_STATE( OFF_STATE ),
342 POWER_STATE( RESTART_STATE ),
343 POWER_STATE( SLEEP_STATE ),
344 POWER_STATE( AOT_STATE ),
345 POWER_STATE( ON_STATE ),
346 { 0, NULL }
347 };
348 return IOFindNameForValue(state, powerStates);
349 }
350
351 #define ON_POWER kIOPMPowerOn
352 #define RESTART_POWER kIOPMRestart
353 #define SLEEP_POWER kIOPMAuxPowerOn
354
355 static IOPMPowerState
356 ourPowerStates[NUM_POWER_STATES] =
357 {
358 { .version = 1,
359 .capabilityFlags = 0,
360 .outputPowerCharacter = 0,
361 .inputPowerRequirement = 0 },
362 { .version = 1,
363 .capabilityFlags = kIOPMRestartCapability,
364 .outputPowerCharacter = kIOPMRestart,
365 .inputPowerRequirement = RESTART_POWER },
366 { .version = 1,
367 .capabilityFlags = kIOPMSleepCapability,
368 .outputPowerCharacter = kIOPMSleep,
369 .inputPowerRequirement = SLEEP_POWER },
370 { .version = 1,
371 .capabilityFlags = kIOPMAOTCapability,
372 .outputPowerCharacter = kIOPMAOTPower,
373 .inputPowerRequirement = ON_POWER },
374 { .version = 1,
375 .capabilityFlags = kIOPMPowerOn,
376 .outputPowerCharacter = kIOPMPowerOn,
377 .inputPowerRequirement = ON_POWER },
378 };
379
380 #define kIOPMRootDomainWakeTypeSleepService "SleepService"
381 #define kIOPMRootDomainWakeTypeMaintenance "Maintenance"
382 #define kIOPMRootDomainWakeTypeSleepTimer "SleepTimer"
383 #define kIOPMrootDomainWakeTypeLowBattery "LowBattery"
384 #define kIOPMRootDomainWakeTypeUser "User"
385 #define kIOPMRootDomainWakeTypeAlarm "Alarm"
386 #define kIOPMRootDomainWakeTypeNetwork "Network"
387 #define kIOPMRootDomainWakeTypeHIDActivity "HID Activity"
388 #define kIOPMRootDomainWakeTypeNotification "Notification"
389 #define kIOPMRootDomainWakeTypeHibernateError "HibernateError"
390
391 // Special interest that entitles the interested client from receiving
392 // all system messages. Only used by powerd.
393 //
394 #define kIOPMSystemCapabilityInterest "IOPMSystemCapabilityInterest"
395
396 // Entitlement required for root domain clients
397 #define kRootDomainEntitlementSetProperty "com.apple.private.iokit.rootdomain-set-property"
398
399 #define WAKEEVENT_LOCK() IOLockLock(wakeEventLock)
400 #define WAKEEVENT_UNLOCK() IOLockUnlock(wakeEventLock)
401
402 /*
403 * Aggressiveness
404 */
405 #define AGGRESSIVES_LOCK() IOLockLock(featuresDictLock)
406 #define AGGRESSIVES_UNLOCK() IOLockUnlock(featuresDictLock)
407
408 #define kAggressivesMinValue 1
409
410 const char *
getAggressivenessTypeString(uint32_t type)411 getAggressivenessTypeString( uint32_t type )
412 {
413 #define AGGRESSIVENESS_TYPE(x) {(uint32_t) x, #x}
414
415 static const IONamedValue aggressivenessTypes[] = {
416 AGGRESSIVENESS_TYPE( kPMGeneralAggressiveness ),
417 AGGRESSIVENESS_TYPE( kPMMinutesToDim ),
418 AGGRESSIVENESS_TYPE( kPMMinutesToSpinDown ),
419 AGGRESSIVENESS_TYPE( kPMMinutesToSleep ),
420 AGGRESSIVENESS_TYPE( kPMEthernetWakeOnLANSettings ),
421 AGGRESSIVENESS_TYPE( kPMSetProcessorSpeed ),
422 AGGRESSIVENESS_TYPE( kPMPowerSource),
423 AGGRESSIVENESS_TYPE( kPMMotionSensor ),
424 AGGRESSIVENESS_TYPE( kPMLastAggressivenessType ),
425 { 0, NULL }
426 };
427 return IOFindNameForValue(type, aggressivenessTypes);
428 }
429
430 enum {
431 kAggressivesStateBusy = 0x01,
432 kAggressivesStateQuickSpindown = 0x02
433 };
434
435 struct AggressivesRecord {
436 uint32_t flags;
437 uint32_t type;
438 uint32_t value;
439 };
440
441 struct AggressivesRequest {
442 queue_chain_t chain;
443 uint32_t options;
444 uint32_t dataType;
445 union {
446 OSSharedPtr<IOService> service;
447 AggressivesRecord record;
448 } data;
449 };
450
451 enum {
452 kAggressivesRequestTypeService = 1,
453 kAggressivesRequestTypeRecord
454 };
455
456 enum {
457 kAggressivesOptionSynchronous = 0x00000001,
458 kAggressivesOptionQuickSpindownEnable = 0x00000100,
459 kAggressivesOptionQuickSpindownDisable = 0x00000200,
460 kAggressivesOptionQuickSpindownMask = 0x00000300
461 };
462
463 enum {
464 kAggressivesRecordFlagModified = 0x00000001,
465 kAggressivesRecordFlagMinValue = 0x00000002
466 };
467
468 // System Sleep Preventers
469
470 enum {
471 kPMUserDisabledAllSleep = 1,
472 kPMSystemRestartBootingInProgress,
473 kPMConfigPreventSystemSleep,
474 kPMChildPreventSystemSleep,
475 kPMCPUAssertion,
476 kPMPCIUnsupported,
477 };
478
479 const char *
getSystemSleepPreventerString(uint32_t preventer)480 getSystemSleepPreventerString( uint32_t preventer )
481 {
482 #define SYSTEM_SLEEP_PREVENTER(x) {(int) x, #x}
483 static const IONamedValue systemSleepPreventers[] = {
484 SYSTEM_SLEEP_PREVENTER( kPMUserDisabledAllSleep ),
485 SYSTEM_SLEEP_PREVENTER( kPMSystemRestartBootingInProgress ),
486 SYSTEM_SLEEP_PREVENTER( kPMConfigPreventSystemSleep ),
487 SYSTEM_SLEEP_PREVENTER( kPMChildPreventSystemSleep ),
488 SYSTEM_SLEEP_PREVENTER( kPMCPUAssertion ),
489 SYSTEM_SLEEP_PREVENTER( kPMPCIUnsupported ),
490 { 0, NULL }
491 };
492 return IOFindNameForValue(preventer, systemSleepPreventers);
493 }
494
495 // gDarkWakeFlags
496 enum {
497 kDarkWakeFlagPromotionNone = 0x0000,
498 kDarkWakeFlagPromotionEarly = 0x0001, // promote before gfx clamp
499 kDarkWakeFlagPromotionLate = 0x0002, // promote after gfx clamp
500 kDarkWakeFlagPromotionMask = 0x0003,
501 kDarkWakeFlagAlarmIsDark = 0x0100,
502 kDarkWakeFlagAudioNotSuppressed = 0x0200,
503 kDarkWakeFlagUserWakeWorkaround = 0x1000
504 };
505
506 // gClamshellFlags
507 // The workaround for 9157444 is enabled at compile time using the
508 // DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY macro and is not represented below.
509 enum {
510 kClamshell_WAR_38378787 = 0x00000001,
511 kClamshell_WAR_47715679 = 0x00000002,
512 kClamshell_WAR_58009435 = 0x00000004
513 };
514
515 // acceptSystemWakeEvents()
516 enum {
517 kAcceptSystemWakeEvents_Disable = 0,
518 kAcceptSystemWakeEvents_Enable,
519 kAcceptSystemWakeEvents_Reenable
520 };
521
522 static IOPMrootDomain * gRootDomain;
523 static IORootParent * gPatriarch;
524 static IONotifier * gSysPowerDownNotifier = NULL;
525 static UInt32 gSleepOrShutdownPending = 0;
526 static UInt32 gWillShutdown = 0;
527 static UInt32 gPagingOff = 0;
528 static UInt32 gSleepWakeUUIDIsSet = false;
529 static uint32_t gAggressivesState = 0;
530 uint32_t gHaltTimeMaxLog;
531 uint32_t gHaltTimeMaxPanic;
532 IOLock * gHaltLogLock;
533 static char * gHaltLog;
534 enum { kHaltLogSize = 2048 };
535 static size_t gHaltLogPos;
536 static uint64_t gHaltStartTime;
537 static char gKextNameBuf[64];
538 static size_t gKextNamePos;
539 static bool gKextNameEnd;
540
541 uuid_string_t bootsessionuuid_string;
542
543 #if defined(XNU_TARGET_OS_OSX)
544 #if DISPLAY_WRANGLER_PRESENT
545 static uint32_t gDarkWakeFlags = kDarkWakeFlagPromotionNone;
546 #elif defined(__arm64__)
547 // Enable temporary full wake promotion workarounds
548 static uint32_t gDarkWakeFlags = kDarkWakeFlagUserWakeWorkaround;
549 #else
550 // Enable full wake promotion workarounds
551 static uint32_t gDarkWakeFlags = kDarkWakeFlagUserWakeWorkaround;
552 #endif
553 #else /* !defined(XNU_TARGET_OS_OSX) */
554 static uint32_t gDarkWakeFlags = kDarkWakeFlagPromotionEarly;
555 #endif /* !defined(XNU_TARGET_OS_OSX) */
556
557 static uint32_t gNoIdleFlag = 0;
558 static uint32_t gSleepDisabledFlag = 0;
559 static uint32_t gSwdPanic = 1;
560 static uint32_t gSwdSleepTimeout = 0;
561 static uint32_t gSwdWakeTimeout = 0;
562 static uint32_t gSwdSleepWakeTimeout = 0;
563 static PMStatsStruct gPMStats;
564 #if DEVELOPMENT || DEBUG
565 static uint32_t swd_panic_phase;
566 #endif
567
568 static uint32_t gClamshellFlags = 0
569 #if defined(__i386__) || defined(__x86_64__)
570 | kClamshell_WAR_58009435
571 #endif
572 ;
573
574 #if HIBERNATION
575
576 #if defined(__arm64__)
577 static IOReturn
defaultSleepPolicyHandler(void * ctx,const IOPMSystemSleepPolicyVariables * vars,IOPMSystemSleepParameters * params)578 defaultSleepPolicyHandler(void *ctx, const IOPMSystemSleepPolicyVariables *vars, IOPMSystemSleepParameters *params)
579 {
580 uint32_t sleepType = kIOPMSleepTypeDeepIdle;
581
582 assert(vars->signature == kIOPMSystemSleepPolicySignature);
583 assert(vars->version == kIOPMSystemSleepPolicyVersion);
584
585 // Hibernation enabled and either user forced hibernate or low battery sleep
586 if ((vars->hibernateMode & kIOHibernateModeOn) &&
587 (((vars->hibernateMode & kIOHibernateModeSleep) == 0) ||
588 (vars->sleepFactors & kIOPMSleepFactorBatteryLow))) {
589 sleepType = kIOPMSleepTypeHibernate;
590 }
591 params->version = kIOPMSystemSleepParametersVersion;
592 params->sleepType = sleepType;
593 return kIOReturnSuccess;
594 }
595 static IOPMSystemSleepPolicyHandler gSleepPolicyHandler = &defaultSleepPolicyHandler;
596 #else /* defined(__arm64__) */
597 static IOPMSystemSleepPolicyHandler gSleepPolicyHandler = NULL;
598 #endif /* defined(__arm64__) */
599
600 static IOPMSystemSleepPolicyVariables * gSleepPolicyVars = NULL;
601 static void * gSleepPolicyTarget;
602 #endif
603
604 struct timeval gIOLastSleepTime;
605 struct timeval gIOLastWakeTime;
606 AbsoluteTime gIOLastWakeAbsTime;
607 AbsoluteTime gIOLastSleepAbsTime;
608
609 struct timeval gIOLastUserSleepTime;
610
611 static char gWakeReasonString[128];
612 static char gBootReasonString[80];
613 static char gShutdownReasonString[80];
614 static bool gWakeReasonSysctlRegistered = false;
615 static bool gBootReasonSysctlRegistered = false;
616 static bool gShutdownReasonSysctlRegistered = false;
617 static bool gWillShutdownSysctlRegistered = false;
618 static AbsoluteTime gUserActiveAbsTime;
619 static AbsoluteTime gUserInactiveAbsTime;
620
621 #if defined(__i386__) || defined(__x86_64__) || (defined(__arm64__) && HIBERNATION)
622 static bool gSpinDumpBufferFull = false;
623 #endif
624
625 z_stream swd_zs;
626 vm_offset_t swd_zs_zmem;
627 //size_t swd_zs_zsize;
628 size_t swd_zs_zoffset;
629 #if defined(__i386__) || defined(__x86_64__)
630 IOCPU *currentShutdownTarget = NULL;
631 #endif
632
633 static unsigned int gPMHaltBusyCount;
634 static unsigned int gPMHaltIdleCount;
635 static int gPMHaltDepth;
636 static uint32_t gPMHaltMessageType;
637 static IOLock * gPMHaltLock = NULL;
638 static OSSharedPtr<OSArray> gPMHaltArray;
639 static OSSharedPtr<const OSSymbol> gPMHaltClientAcknowledgeKey;
640 static bool gPMQuiesced;
641
642 // Constants used as arguments to IOPMrootDomain::informCPUStateChange
643 #define kCPUUnknownIndex 9999999
644 enum {
645 kInformAC = 0,
646 kInformLid = 1,
647 kInformableCount = 2
648 };
649
650 OSSharedPtr<const OSSymbol> gIOPMStatsResponseTimedOut;
651 OSSharedPtr<const OSSymbol> gIOPMStatsResponseCancel;
652 OSSharedPtr<const OSSymbol> gIOPMStatsResponseSlow;
653 OSSharedPtr<const OSSymbol> gIOPMStatsResponsePrompt;
654 OSSharedPtr<const OSSymbol> gIOPMStatsDriverPSChangeSlow;
655
656 #define kBadPMFeatureID 0
657
658 /*
659 * PMSettingHandle
660 * Opaque handle passed to clients of registerPMSettingController()
661 */
662 class PMSettingHandle : public OSObject
663 {
664 OSDeclareFinalStructors( PMSettingHandle );
665 friend class PMSettingObject;
666
667 private:
668 PMSettingObject *pmso;
669 void free(void) APPLE_KEXT_OVERRIDE;
670 };
671
672 /*
673 * PMSettingObject
674 * Internal object to track each PM setting controller
675 */
676 class PMSettingObject : public OSObject
677 {
678 OSDeclareFinalStructors( PMSettingObject );
679 friend class IOPMrootDomain;
680
681 private:
682 queue_head_t calloutQueue;
683 thread_t waitThread;
684 IOPMrootDomain *parent;
685 PMSettingHandle *pmsh;
686 IOPMSettingControllerCallback func;
687 OSObject *target;
688 uintptr_t refcon;
689 OSDataAllocation<uint32_t> publishedFeatureID;
690 uint32_t settingCount;
691 bool disabled;
692
693 void free(void) APPLE_KEXT_OVERRIDE;
694
695 public:
696 static PMSettingObject *pmSettingObject(
697 IOPMrootDomain *parent_arg,
698 IOPMSettingControllerCallback handler_arg,
699 OSObject *target_arg,
700 uintptr_t refcon_arg,
701 uint32_t supportedPowerSources,
702 const OSSymbol *settings[],
703 OSObject **handle_obj);
704
705 IOReturn dispatchPMSetting(const OSSymbol *type, OSObject *object);
706 void clientHandleFreed(void);
707 };
708
709 struct PMSettingCallEntry {
710 queue_chain_t link;
711 thread_t thread;
712 };
713
714 #define PMSETTING_LOCK() IOLockLock(settingsCtrlLock)
715 #define PMSETTING_UNLOCK() IOLockUnlock(settingsCtrlLock)
716 #define PMSETTING_WAIT(p) IOLockSleep(settingsCtrlLock, p, THREAD_UNINT)
717 #define PMSETTING_WAKEUP(p) IOLockWakeup(settingsCtrlLock, p, true)
718
719 /*
720 * PMTraceWorker
721 * Internal helper object for logging trace points to RTC
722 * IOPMrootDomain and only IOPMrootDomain should instantiate
723 * exactly one of these.
724 */
725
726 typedef void (*IOPMTracePointHandler)(
727 void * target, uint32_t code, uint32_t data );
728
729 class PMTraceWorker : public OSObject
730 {
731 OSDeclareDefaultStructors(PMTraceWorker);
732 public:
733 typedef enum { kPowerChangeStart, kPowerChangeCompleted } change_t;
734
735 static OSPtr<PMTraceWorker> tracer( IOPMrootDomain * );
736 void tracePCIPowerChange(change_t, IOService *, uint32_t, uint32_t);
737 void tracePoint(uint8_t phase);
738 void traceDetail(uint32_t detail);
739 void traceComponentWakeProgress(uint32_t component, uint32_t data);
740 int recordTopLevelPCIDevice(IOService *);
741 void RTC_TRACE(void);
742 virtual bool serialize(OSSerialize *s) const APPLE_KEXT_OVERRIDE;
743
744 IOPMTracePointHandler tracePointHandler;
745 void * tracePointTarget;
746 uint64_t getPMStatusCode();
747 uint8_t getTracePhase();
748 uint32_t getTraceData();
749 private:
750 IOPMrootDomain *owner;
751 IOLock *pmTraceWorkerLock;
752 OSSharedPtr<OSArray> pciDeviceBitMappings;
753
754 uint8_t addedToRegistry;
755 uint8_t tracePhase;
756 uint32_t traceData32;
757 uint8_t loginWindowData;
758 uint8_t coreDisplayData;
759 uint8_t coreGraphicsData;
760 };
761
762 /*
763 * this should be treated as POD, as it's byte-copied around
764 * and we cannot rely on d'tor firing at the right time
765 */
766 struct PMAssertStruct {
767 IOPMDriverAssertionID id;
768 IOPMDriverAssertionType assertionBits;
769 uint64_t createdTime;
770 uint64_t modifiedTime;
771 const OSSymbol *ownerString;
772 IOService *ownerService;
773 uint64_t registryEntryID;
774 IOPMDriverAssertionLevel level;
775 uint64_t assertCPUStartTime;
776 uint64_t assertCPUDuration;
777 };
778 OSDefineValueObjectForDependentType(PMAssertStruct)
779
780 /*
781 * PMAssertionsTracker
782 * Tracks kernel and user space PM assertions
783 */
784 class PMAssertionsTracker : public OSObject
785 {
786 OSDeclareFinalStructors(PMAssertionsTracker);
787 public:
788 static PMAssertionsTracker *pmAssertionsTracker( IOPMrootDomain * );
789
790 IOReturn createAssertion(IOPMDriverAssertionType, IOPMDriverAssertionLevel, IOService *, const char *, IOPMDriverAssertionID *);
791 IOReturn releaseAssertion(IOPMDriverAssertionID);
792 IOReturn setAssertionLevel(IOPMDriverAssertionID, IOPMDriverAssertionLevel);
793 IOReturn setUserAssertionLevels(IOPMDriverAssertionType);
794
795 OSSharedPtr<OSArray> copyAssertionsArray(void);
796 IOPMDriverAssertionType getActivatedAssertions(void);
797 IOPMDriverAssertionLevel getAssertionLevel(IOPMDriverAssertionType);
798
799 IOReturn handleCreateAssertion(OSValueObject<PMAssertStruct> *);
800 IOReturn handleReleaseAssertion(IOPMDriverAssertionID);
801 IOReturn handleSetAssertionLevel(IOPMDriverAssertionID, IOPMDriverAssertionLevel);
802 IOReturn handleSetUserAssertionLevels(void * arg0);
803 void publishProperties(void);
804 void reportCPUBitAccounting(void);
805
806 private:
807 uint32_t tabulateProducerCount;
808 uint32_t tabulateConsumerCount;
809
810 uint64_t maxAssertCPUDuration;
811 uint64_t maxAssertCPUEntryId;
812
813 PMAssertStruct *detailsForID(IOPMDriverAssertionID, int *);
814 void tabulate(void);
815 void updateCPUBitAccounting(PMAssertStruct * assertStruct);
816
817 IOPMrootDomain *owner;
818 OSSharedPtr<OSArray> assertionsArray;
819 IOLock *assertionsArrayLock;
820 IOPMDriverAssertionID issuingUniqueID __attribute__((aligned(8)));/* aligned for atomic access */
821 IOPMDriverAssertionType assertionsKernel;
822 IOPMDriverAssertionType assertionsUser;
823 IOPMDriverAssertionType assertionsCombined;
824 };
825
826 OSDefineMetaClassAndFinalStructors(PMAssertionsTracker, OSObject);
827
828 /*
829 * PMHaltWorker
830 * Internal helper object for Shutdown/Restart notifications.
831 */
832 #define kPMHaltMaxWorkers 8
833 #define kPMHaltTimeoutMS 100
834
835 class PMHaltWorker : public OSObject
836 {
837 OSDeclareFinalStructors( PMHaltWorker );
838
839 public:
840 IOService * service;// service being worked on
841 AbsoluteTime startTime; // time when work started
842 int depth; // work on nubs at this PM-tree depth
843 int visits; // number of nodes visited (debug)
844 IOLock * lock;
845 bool timeout;// service took too long
846
847 static PMHaltWorker * worker( void );
848 static void main( void * arg, wait_result_t waitResult );
849 static void work( PMHaltWorker * me );
850 static void checkTimeout( PMHaltWorker * me, AbsoluteTime * now );
851 virtual void free( void ) APPLE_KEXT_OVERRIDE;
852 };
853
OSDefineMetaClassAndFinalStructors(PMHaltWorker,OSObject)854 OSDefineMetaClassAndFinalStructors( PMHaltWorker, OSObject )
855
856
857 #define super IOService
858 OSDefineMetaClassAndFinalStructors(IOPMrootDomain, IOService)
859
860 boolean_t
861 IOPMRootDomainGetWillShutdown(void)
862 {
863 return gWillShutdown != 0;
864 }
865
866 static void
IOPMRootDomainWillShutdown(void)867 IOPMRootDomainWillShutdown(void)
868 {
869 if (OSCompareAndSwap(0, 1, &gWillShutdown)) {
870 IOService::willShutdown();
871 for (int i = 0; i < 100; i++) {
872 if (OSCompareAndSwap(0, 1, &gSleepOrShutdownPending)) {
873 break;
874 }
875 IOSleep( 100 );
876 }
877 }
878 }
879
880 extern "C" IONotifier *
registerSleepWakeInterest(IOServiceInterestHandler handler,void * self,void * ref)881 registerSleepWakeInterest(IOServiceInterestHandler handler, void * self, void * ref)
882 {
883 return gRootDomain->registerInterest( gIOGeneralInterest, handler, self, ref ).detach();
884 }
885
886 extern "C" IONotifier *
registerPrioritySleepWakeInterest(IOServiceInterestHandler handler,void * self,void * ref)887 registerPrioritySleepWakeInterest(IOServiceInterestHandler handler, void * self, void * ref)
888 {
889 return gRootDomain->registerInterest( gIOPriorityPowerStateInterest, handler, self, ref ).detach();
890 }
891
892 extern "C" IOReturn
acknowledgeSleepWakeNotification(void * PMrefcon)893 acknowledgeSleepWakeNotification(void * PMrefcon)
894 {
895 return gRootDomain->allowPowerChange((unsigned long)PMrefcon );
896 }
897
898 extern "C" IOReturn
vetoSleepWakeNotification(void * PMrefcon)899 vetoSleepWakeNotification(void * PMrefcon)
900 {
901 return gRootDomain->cancelPowerChange((unsigned long)PMrefcon );
902 }
903
904 extern "C" IOReturn
rootDomainRestart(void)905 rootDomainRestart( void )
906 {
907 return gRootDomain->restartSystem();
908 }
909
910 extern "C" IOReturn
rootDomainShutdown(void)911 rootDomainShutdown( void )
912 {
913 return gRootDomain->shutdownSystem();
914 }
915
916 static void
halt_log_putc(char c)917 halt_log_putc(char c)
918 {
919 if (gHaltLogPos >= (kHaltLogSize - 2)) {
920 return;
921 }
922 gHaltLog[gHaltLogPos++] = c;
923 }
924
925 extern "C" void
926 _doprnt_log(const char *fmt,
927 va_list *argp,
928 void (*putc)(char),
929 int radix);
930
931 static int
halt_log(const char * fmt,...)932 halt_log(const char *fmt, ...)
933 {
934 va_list listp;
935
936 va_start(listp, fmt);
937 _doprnt_log(fmt, &listp, &halt_log_putc, 16);
938 va_end(listp);
939
940 return 0;
941 }
942
943 extern "C" void
halt_log_enter(const char * what,const void * pc,uint64_t time)944 halt_log_enter(const char * what, const void * pc, uint64_t time)
945 {
946 uint64_t nano, millis;
947
948 if (!gHaltLog) {
949 return;
950 }
951 absolutetime_to_nanoseconds(time, &nano);
952 millis = nano / NSEC_PER_MSEC;
953 if (millis < 100) {
954 return;
955 }
956
957 IOLockLock(gHaltLogLock);
958 if (pc) {
959 halt_log("%s: %qd ms @ 0x%lx, ", what, millis, VM_KERNEL_UNSLIDE(pc));
960 OSKext::printKextsInBacktrace((vm_offset_t *) &pc, 1, &halt_log,
961 OSKext::kPrintKextsLock | OSKext::kPrintKextsUnslide | OSKext::kPrintKextsTerse);
962 } else {
963 halt_log("%s: %qd ms\n", what, millis);
964 }
965
966 gHaltLog[gHaltLogPos] = 0;
967 IOLockUnlock(gHaltLogLock);
968 }
969
970 extern uint32_t gFSState;
971
972 extern "C" void
IOSystemShutdownNotification(int howto,int stage)973 IOSystemShutdownNotification(int howto, int stage)
974 {
975 uint64_t startTime;
976
977 if (kIOSystemShutdownNotificationStageRootUnmount == stage) {
978 #if defined(XNU_TARGET_OS_OSX)
979 uint64_t nano, millis;
980 startTime = mach_absolute_time();
981 IOService::getPlatform()->waitQuiet(30 * NSEC_PER_SEC);
982 absolutetime_to_nanoseconds(mach_absolute_time() - startTime, &nano);
983 millis = nano / NSEC_PER_MSEC;
984 if (gHaltTimeMaxLog && (millis >= gHaltTimeMaxLog)) {
985 printf("waitQuiet() for unmount %qd ms\n", millis);
986 }
987 #endif /* defined(XNU_TARGET_OS_OSX) */
988 return;
989 }
990
991 if (kIOSystemShutdownNotificationTerminateDEXTs == stage) {
992 uint64_t nano, millis;
993 startTime = mach_absolute_time();
994 IOServicePH::systemHalt(howto);
995 absolutetime_to_nanoseconds(mach_absolute_time() - startTime, &nano);
996 millis = nano / NSEC_PER_MSEC;
997 if (true || (gHaltTimeMaxLog && (millis >= gHaltTimeMaxLog))) {
998 printf("IOServicePH::systemHalt took %qd ms\n", millis);
999 }
1000 return;
1001 }
1002
1003 assert(kIOSystemShutdownNotificationStageProcessExit == stage);
1004
1005 IOLockLock(gHaltLogLock);
1006 if (!gHaltLog) {
1007 gHaltLog = IONewData(char, (vm_size_t)kHaltLogSize);
1008 gHaltStartTime = mach_absolute_time();
1009 if (gHaltLog) {
1010 halt_log_putc('\n');
1011 }
1012 }
1013 IOLockUnlock(gHaltLogLock);
1014
1015 startTime = mach_absolute_time();
1016 IOPMRootDomainWillShutdown();
1017 halt_log_enter("IOPMRootDomainWillShutdown", NULL, mach_absolute_time() - startTime);
1018 #if HIBERNATION
1019 startTime = mach_absolute_time();
1020 IOHibernateSystemPostWake(true);
1021 halt_log_enter("IOHibernateSystemPostWake", NULL, mach_absolute_time() - startTime);
1022 #endif
1023 if (OSCompareAndSwap(0, 1, &gPagingOff)) {
1024 gRootDomain->handlePlatformHaltRestart(kPEPagingOff);
1025 }
1026 }
1027
1028 extern "C" int sync_internal(void);
1029
1030 /*
1031 * A device is always in the highest power state which satisfies its driver,
1032 * its policy-maker, and any power children it has, but within the constraint
1033 * of the power state provided by its parent. The driver expresses its desire by
1034 * calling changePowerStateTo(), the policy-maker expresses its desire by calling
1035 * changePowerStateToPriv(), and the children express their desires by calling
1036 * requestPowerDomainState().
1037 *
1038 * The Root Power Domain owns the policy for idle and demand sleep for the system.
1039 * It is a power-managed IOService just like the others in the system.
1040 * It implements several power states which map to what we see as Sleep and On.
1041 *
1042 * The sleep policy is as follows:
1043 * 1. Sleep is prevented if the case is open so that nobody will think the machine
1044 * is off and plug/unplug cards.
1045 * 2. Sleep is prevented if the sleep timeout slider in the prefs panel is zero.
1046 * 3. System cannot Sleep if some object in the tree is in a power state marked
1047 * kIOPMPreventSystemSleep.
1048 *
1049 * These three conditions are enforced using the "driver clamp" by calling
1050 * changePowerStateTo(). For example, if the case is opened,
1051 * changePowerStateTo(ON_STATE) is called to hold the system on regardless
1052 * of the desires of the children of the root or the state of the other clamp.
1053 *
1054 * Demand Sleep is initiated by pressing the front panel power button, closing
1055 * the clamshell, or selecting the menu item. In this case the root's parent
1056 * actually initiates the power state change so that the root domain has no
1057 * choice and does not give applications the opportunity to veto the change.
1058 *
1059 * Idle Sleep occurs if no objects in the tree are in a state marked
1060 * kIOPMPreventIdleSleep. When this is true, the root's children are not holding
1061 * the root on, so it sets the "policy-maker clamp" by calling
1062 * changePowerStateToPriv(ON_STATE) to hold itself on until the sleep timer expires.
1063 * This timer is set for the difference between the sleep timeout slider and the
1064 * display dim timeout slider. When the timer expires, it releases its clamp and
1065 * now nothing is holding it awake, so it falls asleep.
1066 *
1067 * Demand sleep is prevented when the system is booting. When preferences are
1068 * transmitted by the loginwindow at the end of boot, a flag is cleared,
1069 * and this allows subsequent Demand Sleep.
1070 */
1071
1072 //******************************************************************************
1073
1074 IOPMrootDomain *
construct(void)1075 IOPMrootDomain::construct( void )
1076 {
1077 IOPMrootDomain *root;
1078
1079 root = new IOPMrootDomain;
1080 if (root) {
1081 root->init();
1082 }
1083
1084 return root;
1085 }
1086
1087 //******************************************************************************
1088 // updateConsoleUsersCallout
1089 //
1090 //******************************************************************************
1091
1092 static void
updateConsoleUsersCallout(thread_call_param_t p0,thread_call_param_t p1)1093 updateConsoleUsersCallout(thread_call_param_t p0, thread_call_param_t p1)
1094 {
1095 IOPMrootDomain * rootDomain = (IOPMrootDomain *) p0;
1096 rootDomain->updateConsoleUsers();
1097 }
1098
1099 void
updateConsoleUsers(void)1100 IOPMrootDomain::updateConsoleUsers(void)
1101 {
1102 IOService::updateConsoleUsers(NULL, kIOMessageSystemHasPoweredOn);
1103 updateTasksSuspend(kTasksSuspendUnsuspended, kTasksSuspendNoChange);
1104 }
1105
1106 bool
updateTasksSuspend(int newTasksSuspended,int newAOTTasksSuspended)1107 IOPMrootDomain::updateTasksSuspend(int newTasksSuspended, int newAOTTasksSuspended)
1108 {
1109 bool newSuspend;
1110
1111 WAKEEVENT_LOCK();
1112 if (newTasksSuspended != kTasksSuspendNoChange) {
1113 tasksSuspended = (newTasksSuspended != kTasksSuspendUnsuspended);
1114 }
1115 if (newAOTTasksSuspended != kTasksSuspendNoChange) {
1116 _aotTasksSuspended = (newAOTTasksSuspended != kTasksSuspendUnsuspended);
1117 }
1118 newSuspend = (tasksSuspended || _aotTasksSuspended);
1119 if (newSuspend == tasksSuspendState) {
1120 WAKEEVENT_UNLOCK();
1121 return false;
1122 }
1123 tasksSuspendState = newSuspend;
1124 WAKEEVENT_UNLOCK();
1125 tasks_system_suspend(newSuspend);
1126 return true;
1127 }
1128
1129 //******************************************************************************
1130
1131 static void
disk_sync_callout(thread_call_param_t p0,thread_call_param_t p1)1132 disk_sync_callout( thread_call_param_t p0, thread_call_param_t p1 )
1133 {
1134 IOPMrootDomain * rootDomain = (IOPMrootDomain *) p0;
1135 uint32_t notifyRef = (uint32_t)(uintptr_t) p1;
1136 uint32_t powerState = rootDomain->getPowerState();
1137
1138 DLOG("disk_sync_callout ps=%u\n", powerState);
1139
1140 if (ON_STATE == powerState) {
1141 sync_internal();
1142
1143 #if HIBERNATION
1144 // Block sleep until trim issued on previous wake path is completed.
1145 IOHibernateSystemPostWake(true);
1146 #endif
1147 }
1148 #if HIBERNATION
1149 else {
1150 IOHibernateSystemPostWake(false);
1151
1152 rootDomain->sleepWakeDebugSaveSpinDumpFile();
1153 }
1154 #endif
1155
1156 rootDomain->allowPowerChange(notifyRef);
1157 DLOG("disk_sync_callout finish\n");
1158 }
1159
1160 //******************************************************************************
1161 static UInt32
computeDeltaTimeMS(const AbsoluteTime * startTime,AbsoluteTime * elapsedTime)1162 computeDeltaTimeMS( const AbsoluteTime * startTime, AbsoluteTime * elapsedTime )
1163 {
1164 AbsoluteTime endTime;
1165 UInt64 nano = 0;
1166
1167 clock_get_uptime(&endTime);
1168 if (CMP_ABSOLUTETIME(&endTime, startTime) <= 0) {
1169 *elapsedTime = 0;
1170 } else {
1171 SUB_ABSOLUTETIME(&endTime, startTime);
1172 absolutetime_to_nanoseconds(endTime, &nano);
1173 *elapsedTime = endTime;
1174 }
1175
1176 return (UInt32)(nano / NSEC_PER_MSEC);
1177 }
1178
1179 //******************************************************************************
1180
1181 static int
1182 sysctl_sleepwaketime SYSCTL_HANDLER_ARGS
1183 {
1184 struct timeval *swt = (struct timeval *)arg1;
1185 struct proc *p = req->p;
1186
1187 if (p == kernproc) {
1188 return sysctl_io_opaque(req, swt, sizeof(*swt), NULL);
1189 } else if (proc_is64bit(p)) {
1190 struct user64_timeval t = {};
1191 t.tv_sec = swt->tv_sec;
1192 t.tv_usec = swt->tv_usec;
1193 return sysctl_io_opaque(req, &t, sizeof(t), NULL);
1194 } else {
1195 struct user32_timeval t = {};
1196 t.tv_sec = (typeof(t.tv_sec))swt->tv_sec;
1197 t.tv_usec = swt->tv_usec;
1198 return sysctl_io_opaque(req, &t, sizeof(t), NULL);
1199 }
1200 }
1201
1202 static SYSCTL_PROC(_kern, OID_AUTO, sleeptime,
1203 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
1204 &gIOLastUserSleepTime, 0, sysctl_sleepwaketime, "S,timeval", "");
1205
1206 static SYSCTL_PROC(_kern, OID_AUTO, waketime,
1207 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
1208 &gIOLastWakeTime, 0, sysctl_sleepwaketime, "S,timeval", "");
1209
1210 SYSCTL_QUAD(_kern, OID_AUTO, wake_abs_time, CTLFLAG_RD | CTLFLAG_LOCKED, &gIOLastWakeAbsTime, "");
1211 SYSCTL_QUAD(_kern, OID_AUTO, sleep_abs_time, CTLFLAG_RD | CTLFLAG_LOCKED, &gIOLastSleepAbsTime, "");
1212 SYSCTL_QUAD(_kern, OID_AUTO, useractive_abs_time, CTLFLAG_RD | CTLFLAG_LOCKED, &gUserActiveAbsTime, "");
1213 SYSCTL_QUAD(_kern, OID_AUTO, userinactive_abs_time, CTLFLAG_RD | CTLFLAG_LOCKED, &gUserInactiveAbsTime, "");
1214
1215 static int
1216 sysctl_willshutdown SYSCTL_HANDLER_ARGS
1217 {
1218 int new_value, changed, error;
1219
1220 if (!gWillShutdownSysctlRegistered) {
1221 return ENOENT;
1222 }
1223
1224 error = sysctl_io_number(req, gWillShutdown, sizeof(int), &new_value, &changed);
1225 if (changed) {
1226 if (!gWillShutdown && (new_value == 1)) {
1227 IOPMRootDomainWillShutdown();
1228 } else {
1229 error = EINVAL;
1230 }
1231 }
1232 return error;
1233 }
1234
1235 static SYSCTL_PROC(_kern, OID_AUTO, willshutdown,
1236 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
1237 NULL, 0, sysctl_willshutdown, "I", "");
1238
1239 #if defined(XNU_TARGET_OS_OSX)
1240
1241 static int
sysctl_progressmeterenable(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)1242 sysctl_progressmeterenable
1243 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
1244 {
1245 int error;
1246 int new_value, changed;
1247
1248 error = sysctl_io_number(req, vc_progressmeter_enable, sizeof(int), &new_value, &changed);
1249
1250 if (changed) {
1251 vc_enable_progressmeter(new_value);
1252 }
1253
1254 return error;
1255 }
1256
1257 static int
sysctl_progressmeter(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)1258 sysctl_progressmeter
1259 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
1260 {
1261 int error;
1262 int new_value, changed;
1263
1264 error = sysctl_io_number(req, vc_progressmeter_value, sizeof(int), &new_value, &changed);
1265
1266 if (changed) {
1267 vc_set_progressmeter(new_value);
1268 }
1269
1270 return error;
1271 }
1272
1273 static SYSCTL_PROC(_kern, OID_AUTO, progressmeterenable,
1274 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
1275 NULL, 0, sysctl_progressmeterenable, "I", "");
1276
1277 static SYSCTL_PROC(_kern, OID_AUTO, progressmeter,
1278 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
1279 NULL, 0, sysctl_progressmeter, "I", "");
1280
1281 #endif /* defined(XNU_TARGET_OS_OSX) */
1282
1283
1284
1285 static int
sysctl_consoleoptions(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)1286 sysctl_consoleoptions
1287 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
1288 {
1289 int error, changed;
1290 uint32_t new_value;
1291
1292 error = sysctl_io_number(req, vc_user_options.options, sizeof(uint32_t), &new_value, &changed);
1293
1294 if (changed) {
1295 vc_user_options.options = new_value;
1296 }
1297
1298 return error;
1299 }
1300
1301 static SYSCTL_PROC(_kern, OID_AUTO, consoleoptions,
1302 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
1303 NULL, 0, sysctl_consoleoptions, "I", "");
1304
1305
1306 static int
1307 sysctl_progressoptions SYSCTL_HANDLER_ARGS
1308 {
1309 return sysctl_io_opaque(req, &vc_user_options, sizeof(vc_user_options), NULL);
1310 }
1311
1312 static SYSCTL_PROC(_kern, OID_AUTO, progressoptions,
1313 CTLTYPE_STRUCT | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED | CTLFLAG_ANYBODY,
1314 NULL, 0, sysctl_progressoptions, "S,vc_progress_user_options", "");
1315
1316
1317 static int
1318 sysctl_wakereason SYSCTL_HANDLER_ARGS
1319 {
1320 char wr[sizeof(gWakeReasonString)];
1321
1322 wr[0] = '\0';
1323 if (gRootDomain && gWakeReasonSysctlRegistered) {
1324 gRootDomain->copyWakeReasonString(wr, sizeof(wr));
1325 } else {
1326 return ENOENT;
1327 }
1328
1329 return sysctl_io_string(req, wr, 0, 0, NULL);
1330 }
1331
1332 SYSCTL_PROC(_kern, OID_AUTO, wakereason,
1333 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
1334 NULL, 0, sysctl_wakereason, "A", "wakereason");
1335
1336 static int
1337 sysctl_bootreason SYSCTL_HANDLER_ARGS
1338 {
1339 if (!os_atomic_load(&gBootReasonSysctlRegistered, acquire)) {
1340 return ENOENT;
1341 }
1342
1343 return sysctl_io_string(req, gBootReasonString, 0, 0, NULL);
1344 }
1345
1346 SYSCTL_PROC(_kern, OID_AUTO, bootreason,
1347 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
1348 NULL, 0, sysctl_bootreason, "A", "");
1349
1350 static int
1351 sysctl_shutdownreason SYSCTL_HANDLER_ARGS
1352 {
1353 char sr[sizeof(gShutdownReasonString)];
1354
1355 sr[0] = '\0';
1356 if (gRootDomain && gShutdownReasonSysctlRegistered) {
1357 gRootDomain->copyShutdownReasonString(sr, sizeof(sr));
1358 } else {
1359 return ENOENT;
1360 }
1361
1362 return sysctl_io_string(req, sr, 0, 0, NULL);
1363 }
1364
1365 SYSCTL_PROC(_kern, OID_AUTO, shutdownreason,
1366 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
1367 NULL, 0, sysctl_shutdownreason, "A", "shutdownreason");
1368
1369 static int
1370 sysctl_targettype SYSCTL_HANDLER_ARGS
1371 {
1372 IOService * root;
1373 OSSharedPtr<OSObject> obj;
1374 OSData * data;
1375 char tt[32];
1376
1377 tt[0] = '\0';
1378 root = IOService::getServiceRoot();
1379 if (root && (obj = root->copyProperty(gIODTTargetTypeKey))) {
1380 if ((data = OSDynamicCast(OSData, obj.get()))) {
1381 strlcpy(tt, (const char *) data->getBytesNoCopy(), sizeof(tt));
1382 }
1383 }
1384 return sysctl_io_string(req, tt, 0, 0, NULL);
1385 }
1386
1387 SYSCTL_PROC(_hw, OID_AUTO, targettype,
1388 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED,
1389 NULL, 0, sysctl_targettype, "A", "targettype");
1390
1391 static SYSCTL_INT(_debug, OID_AUTO, noidle, CTLFLAG_RW, &gNoIdleFlag, 0, "");
1392 static SYSCTL_INT(_debug, OID_AUTO, swd_sleep_timeout, CTLFLAG_RW, &gSwdSleepTimeout, 0, "");
1393 static SYSCTL_INT(_debug, OID_AUTO, swd_wake_timeout, CTLFLAG_RW, &gSwdWakeTimeout, 0, "");
1394 static SYSCTL_INT(_debug, OID_AUTO, swd_timeout, CTLFLAG_RW, &gSwdSleepWakeTimeout, 0, "");
1395 static SYSCTL_INT(_debug, OID_AUTO, swd_panic, CTLFLAG_RW, &gSwdPanic, 0, "");
1396 #if DEVELOPMENT || DEBUG
1397 static SYSCTL_INT(_debug, OID_AUTO, swd_panic_phase, CTLFLAG_RW, &swd_panic_phase, 0, "");
1398 #if defined(XNU_TARGET_OS_OSX)
1399 static SYSCTL_INT(_debug, OID_AUTO, clamshell, CTLFLAG_RW, &gClamshellFlags, 0, "");
1400 static SYSCTL_INT(_debug, OID_AUTO, darkwake, CTLFLAG_RW, &gDarkWakeFlags, 0, "");
1401 #endif /* defined(XNU_TARGET_OS_OSX) */
1402 #endif /* DEVELOPMENT || DEBUG */
1403
1404 //******************************************************************************
1405 // AOT
1406
1407 static int
1408 sysctl_aotmetrics SYSCTL_HANDLER_ARGS
1409 {
1410 if (NULL == gRootDomain) {
1411 return ENOENT;
1412 }
1413 if (NULL == gRootDomain->_aotMetrics) {
1414 IOPMAOTMetrics nullMetrics = {};
1415 return sysctl_io_opaque(req, &nullMetrics, sizeof(IOPMAOTMetrics), NULL);
1416 }
1417 return sysctl_io_opaque(req, gRootDomain->_aotMetrics, sizeof(IOPMAOTMetrics), NULL);
1418 }
1419
1420 static SYSCTL_PROC(_kern, OID_AUTO, aotmetrics,
1421 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_KERN | CTLFLAG_LOCKED | CTLFLAG_ANYBODY,
1422 NULL, 0, sysctl_aotmetrics, "S,IOPMAOTMetrics", "");
1423
1424
1425 static int
update_aotmode(uint32_t mode)1426 update_aotmode(uint32_t mode)
1427 {
1428 int result;
1429
1430 if (!gIOPMWorkLoop) {
1431 return ENOENT;
1432 }
1433 result = gIOPMWorkLoop->runActionBlock(^IOReturn (void) {
1434 unsigned int oldCount;
1435
1436 if (mode && !gRootDomain->_aotMetrics) {
1437 gRootDomain->_aotMetrics = IOMallocType(IOPMAOTMetrics);
1438 }
1439
1440 oldCount = gRootDomain->idleSleepPreventersCount();
1441 gRootDomain->_aotMode = (mode & kIOPMAOTModeMask);
1442 gRootDomain->updatePreventIdleSleepListInternal(NULL, false, oldCount);
1443 return 0;
1444 });
1445 return result;
1446 }
1447
1448 static int
sysctl_aotmodebits(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)1449 sysctl_aotmodebits
1450 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
1451 {
1452 int error, changed;
1453 uint32_t new_value;
1454
1455 if (NULL == gRootDomain) {
1456 return ENOENT;
1457 }
1458 error = sysctl_io_number(req, gRootDomain->_aotMode, sizeof(uint32_t), &new_value, &changed);
1459 if (changed && gIOPMWorkLoop) {
1460 error = update_aotmode(new_value);
1461 }
1462
1463 return error;
1464 }
1465
1466 static SYSCTL_PROC(_kern, OID_AUTO, aotmodebits,
1467 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED,
1468 NULL, 0, sysctl_aotmodebits, "I", "");
1469
1470 static int
sysctl_aotmode(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)1471 sysctl_aotmode
1472 (__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
1473 {
1474 int error, changed;
1475 uint32_t new_value;
1476
1477 if (NULL == gRootDomain) {
1478 return ENOENT;
1479 }
1480 error = sysctl_io_number(req, gRootDomain->_aotMode, sizeof(uint32_t), &new_value, &changed);
1481 if (changed && gIOPMWorkLoop) {
1482 if (new_value) {
1483 new_value = kIOPMAOTModeDefault; // & ~kIOPMAOTModeRespectTimers;
1484 }
1485 error = update_aotmode(new_value);
1486 }
1487
1488 return error;
1489 }
1490
1491 static SYSCTL_PROC(_kern, OID_AUTO, aotmode,
1492 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_KERN | CTLFLAG_LOCKED | CTLFLAG_ANYBODY,
1493 NULL, 0, sysctl_aotmode, "I", "");
1494
1495 //******************************************************************************
1496
1497 static OSSharedPtr<const OSSymbol> gIOPMSettingAutoWakeCalendarKey;
1498 static OSSharedPtr<const OSSymbol> gIOPMSettingAutoWakeSecondsKey;
1499 static OSSharedPtr<const OSSymbol> gIOPMSettingAutoPowerCalendarKey;
1500 static OSSharedPtr<const OSSymbol> gIOPMSettingAutoPowerSecondsKey;
1501 static OSSharedPtr<const OSSymbol> gIOPMSettingDebugWakeRelativeKey;
1502 static OSSharedPtr<const OSSymbol> gIOPMSettingDebugPowerRelativeKey;
1503 static OSSharedPtr<const OSSymbol> gIOPMSettingMaintenanceWakeCalendarKey;
1504 static OSSharedPtr<const OSSymbol> gIOPMSettingSleepServiceWakeCalendarKey;
1505 static OSSharedPtr<const OSSymbol> gIOPMSettingSilentRunningKey;
1506 static OSSharedPtr<const OSSymbol> gIOPMUserTriggeredFullWakeKey;
1507 static OSSharedPtr<const OSSymbol> gIOPMUserIsActiveKey;
1508 static OSSharedPtr<const OSSymbol> gIOPMSettingLowLatencyAudioModeKey;
1509
1510 //******************************************************************************
1511 // start
1512 //
1513 //******************************************************************************
1514
1515 #define kRootDomainSettingsCount 20
1516 #define kRootDomainNoPublishSettingsCount 4
1517
1518 bool
start(IOService * nub)1519 IOPMrootDomain::start( IOService * nub )
1520 {
1521 OSSharedPtr<OSIterator> psIterator;
1522 OSSharedPtr<OSDictionary> tmpDict;
1523
1524 super::start(nub);
1525
1526 gRootDomain = this;
1527 gIOPMSettingAutoWakeCalendarKey = OSSymbol::withCString(kIOPMSettingAutoWakeCalendarKey);
1528 gIOPMSettingAutoWakeSecondsKey = OSSymbol::withCString(kIOPMSettingAutoWakeSecondsKey);
1529 gIOPMSettingAutoPowerCalendarKey = OSSymbol::withCString(kIOPMSettingAutoPowerCalendarKey);
1530 gIOPMSettingAutoPowerSecondsKey = OSSymbol::withCString(kIOPMSettingAutoPowerSecondsKey);
1531 gIOPMSettingDebugWakeRelativeKey = OSSymbol::withCString(kIOPMSettingDebugWakeRelativeKey);
1532 gIOPMSettingDebugPowerRelativeKey = OSSymbol::withCString(kIOPMSettingDebugPowerRelativeKey);
1533 gIOPMSettingMaintenanceWakeCalendarKey = OSSymbol::withCString(kIOPMSettingMaintenanceWakeCalendarKey);
1534 gIOPMSettingSleepServiceWakeCalendarKey = OSSymbol::withCString(kIOPMSettingSleepServiceWakeCalendarKey);
1535 gIOPMSettingSilentRunningKey = OSSymbol::withCStringNoCopy(kIOPMSettingSilentRunningKey);
1536 gIOPMUserTriggeredFullWakeKey = OSSymbol::withCStringNoCopy(kIOPMUserTriggeredFullWakeKey);
1537 gIOPMUserIsActiveKey = OSSymbol::withCStringNoCopy(kIOPMUserIsActiveKey);
1538 gIOPMSettingLowLatencyAudioModeKey = OSSymbol::withCStringNoCopy(kIOPMSettingLowLatencyAudioModeKey);
1539
1540 gIOPMStatsResponseTimedOut = OSSymbol::withCString(kIOPMStatsResponseTimedOut);
1541 gIOPMStatsResponseCancel = OSSymbol::withCString(kIOPMStatsResponseCancel);
1542 gIOPMStatsResponseSlow = OSSymbol::withCString(kIOPMStatsResponseSlow);
1543 gIOPMStatsResponsePrompt = OSSymbol::withCString(kIOPMStatsResponsePrompt);
1544 gIOPMStatsDriverPSChangeSlow = OSSymbol::withCString(kIOPMStatsDriverPSChangeSlow);
1545
1546 sleepSupportedPEFunction = OSSymbol::withCString("IOPMSetSleepSupported");
1547 sleepMessagePEFunction = OSSymbol::withCString("IOPMSystemSleepMessage");
1548 gIOPMWakeTypeUserKey = OSSymbol::withCStringNoCopy(kIOPMRootDomainWakeTypeUser);
1549
1550 OSSharedPtr<const OSSymbol> settingsArr[kRootDomainSettingsCount] =
1551 {
1552 OSSymbol::withCString(kIOPMSettingSleepOnPowerButtonKey),
1553 gIOPMSettingAutoWakeSecondsKey,
1554 gIOPMSettingAutoPowerSecondsKey,
1555 gIOPMSettingAutoWakeCalendarKey,
1556 gIOPMSettingAutoPowerCalendarKey,
1557 gIOPMSettingDebugWakeRelativeKey,
1558 gIOPMSettingDebugPowerRelativeKey,
1559 OSSymbol::withCString(kIOPMSettingWakeOnRingKey),
1560 OSSymbol::withCString(kIOPMSettingRestartOnPowerLossKey),
1561 OSSymbol::withCString(kIOPMSettingWakeOnClamshellKey),
1562 OSSymbol::withCString(kIOPMSettingWakeOnACChangeKey),
1563 OSSymbol::withCString(kIOPMSettingTimeZoneOffsetKey),
1564 OSSymbol::withCString(kIOPMSettingDisplaySleepUsesDimKey),
1565 OSSymbol::withCString(kIOPMSettingMobileMotionModuleKey),
1566 OSSymbol::withCString(kIOPMSettingGraphicsSwitchKey),
1567 OSSymbol::withCString(kIOPMStateConsoleShutdown),
1568 OSSymbol::withCString(kIOPMSettingProModeControl),
1569 OSSymbol::withCString(kIOPMSettingProModeDefer),
1570 gIOPMSettingSilentRunningKey,
1571 gIOPMSettingLowLatencyAudioModeKey,
1572 };
1573
1574 OSSharedPtr<const OSSymbol> noPublishSettingsArr[kRootDomainNoPublishSettingsCount] =
1575 {
1576 OSSymbol::withCString(kIOPMSettingProModeControl),
1577 OSSymbol::withCString(kIOPMSettingProModeDefer),
1578 gIOPMSettingSilentRunningKey,
1579 gIOPMSettingLowLatencyAudioModeKey,
1580 };
1581
1582 #if DEVELOPMENT || DEBUG
1583 #if defined(XNU_TARGET_OS_OSX)
1584 PE_parse_boot_argn("darkwake", &gDarkWakeFlags, sizeof(gDarkWakeFlags));
1585 PE_parse_boot_argn("clamshell", &gClamshellFlags, sizeof(gClamshellFlags));
1586 #endif /* defined(XNU_TARGET_OS_OSX) */
1587 #endif /* DEVELOPMENT || DEBUG */
1588
1589 PE_parse_boot_argn("noidle", &gNoIdleFlag, sizeof(gNoIdleFlag));
1590 PE_parse_boot_argn("swd_sleeptimeout", &gSwdSleepTimeout, sizeof(gSwdSleepTimeout));
1591 PE_parse_boot_argn("swd_waketimeout", &gSwdWakeTimeout, sizeof(gSwdWakeTimeout));
1592 PE_parse_boot_argn("swd_timeout", &gSwdSleepWakeTimeout, sizeof(gSwdSleepWakeTimeout));
1593 PE_parse_boot_argn("haltmspanic", &gHaltTimeMaxPanic, sizeof(gHaltTimeMaxPanic));
1594 PE_parse_boot_argn("haltmslog", &gHaltTimeMaxLog, sizeof(gHaltTimeMaxLog));
1595
1596 // read noidle setting from Device Tree
1597 if (PE_get_default("no-idle", &gNoIdleFlag, sizeof(gNoIdleFlag))) {
1598 DLOG("Setting gNoIdleFlag to %u from device tree\n", gNoIdleFlag);
1599 }
1600
1601 queue_init(&aggressivesQueue);
1602 aggressivesThreadCall = thread_call_allocate(handleAggressivesFunction, this);
1603 aggressivesData = OSData::withCapacity(
1604 sizeof(AggressivesRecord) * (kPMLastAggressivenessType + 4));
1605
1606 featuresDictLock = IOLockAlloc();
1607 settingsCtrlLock = IOLockAlloc();
1608 wakeEventLock = IOLockAlloc();
1609 gHaltLogLock = IOLockAlloc();
1610
1611 extraSleepTimer = thread_call_allocate(
1612 idleSleepTimerExpired,
1613 (thread_call_param_t) this);
1614
1615 powerButtonDown = thread_call_allocate(
1616 powerButtonDownCallout,
1617 (thread_call_param_t) this);
1618
1619 powerButtonUp = thread_call_allocate(
1620 powerButtonUpCallout,
1621 (thread_call_param_t) this);
1622
1623 diskSyncCalloutEntry = thread_call_allocate(
1624 &disk_sync_callout,
1625 (thread_call_param_t) this);
1626 updateConsoleUsersEntry = thread_call_allocate(
1627 &updateConsoleUsersCallout,
1628 (thread_call_param_t) this);
1629
1630 #if DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY
1631 fullWakeThreadCall = thread_call_allocate_with_options(
1632 OSMemberFunctionCast(thread_call_func_t, this,
1633 &IOPMrootDomain::fullWakeDelayedWork),
1634 (thread_call_param_t) this, THREAD_CALL_PRIORITY_KERNEL,
1635 THREAD_CALL_OPTIONS_ONCE);
1636 #endif
1637
1638 setProperty(kIOSleepSupportedKey, true);
1639
1640 bzero(&gPMStats, sizeof(gPMStats));
1641
1642 pmTracer = PMTraceWorker::tracer(this);
1643
1644 pmAssertions = PMAssertionsTracker::pmAssertionsTracker(this);
1645
1646 userDisabledAllSleep = false;
1647 systemBooting = true;
1648 idleSleepEnabled = false;
1649 sleepSlider = 0;
1650 idleSleepTimerPending = false;
1651 wrangler = NULL;
1652 clamshellClosed = false;
1653 clamshellExists = false;
1654 #if DISPLAY_WRANGLER_PRESENT
1655 clamshellDisabled = true;
1656 #else
1657 clamshellDisabled = false;
1658 #endif
1659 clamshellIgnoreClose = false;
1660 acAdaptorConnected = true;
1661 clamshellSleepDisableMask = 0;
1662 gWakeReasonString[0] = '\0';
1663
1664 // Initialize to user active.
1665 // Will never transition to user inactive w/o wrangler.
1666 fullWakeReason = kFullWakeReasonLocalUser;
1667 userIsActive = userWasActive = true;
1668 clock_get_uptime(&gUserActiveAbsTime);
1669 setProperty(gIOPMUserIsActiveKey.get(), kOSBooleanTrue);
1670
1671 // Set the default system capabilities at boot.
1672 _currentCapability = kIOPMSystemCapabilityCPU |
1673 kIOPMSystemCapabilityGraphics |
1674 kIOPMSystemCapabilityAudio |
1675 kIOPMSystemCapabilityNetwork;
1676
1677 _pendingCapability = _currentCapability;
1678 _desiredCapability = _currentCapability;
1679 _highestCapability = _currentCapability;
1680 setProperty(kIOPMSystemCapabilitiesKey, _currentCapability, 64);
1681
1682 queuedSleepWakeUUIDString = NULL;
1683 initializeBootSessionUUID();
1684 pmStatsAppResponses = OSArray::withCapacity(5);
1685 _statsNameKey = OSSymbol::withCString(kIOPMStatsNameKey);
1686 _statsPIDKey = OSSymbol::withCString(kIOPMStatsPIDKey);
1687 _statsTimeMSKey = OSSymbol::withCString(kIOPMStatsTimeMSKey);
1688 _statsResponseTypeKey = OSSymbol::withCString(kIOPMStatsApplicationResponseTypeKey);
1689 _statsMessageTypeKey = OSSymbol::withCString(kIOPMStatsMessageTypeKey);
1690 _statsPowerCapsKey = OSSymbol::withCString(kIOPMStatsPowerCapabilityKey);
1691 assertOnWakeSecs = -1;// Invalid value to prevent updates
1692
1693 pmStatsLock = IOLockAlloc();
1694 idxPMCPUClamshell = kCPUUnknownIndex;
1695 idxPMCPULimitedPower = kCPUUnknownIndex;
1696
1697 tmpDict = OSDictionary::withCapacity(1);
1698 setProperty(kRootDomainSupportedFeatures, tmpDict.get());
1699
1700 // Set a default "SystemPowerProfileOverrideDict" for platform
1701 // drivers without any overrides.
1702 if (!propertyExists(kIOPMSystemDefaultOverrideKey)) {
1703 tmpDict = OSDictionary::withCapacity(1);
1704 setProperty(kIOPMSystemDefaultOverrideKey, tmpDict.get());
1705 }
1706
1707 settingsCallbacks = OSDictionary::withCapacity(1);
1708
1709 // Create a list of the valid PM settings that we'll relay to
1710 // interested clients in setProperties() => setPMSetting()
1711 allowedPMSettings = OSArray::withObjects(
1712 (const OSObject **)settingsArr,
1713 kRootDomainSettingsCount,
1714 0);
1715
1716 // List of PM settings that should not automatically publish itself
1717 // as a feature when registered by a listener.
1718 noPublishPMSettings = OSArray::withObjects(
1719 (const OSObject **)noPublishSettingsArr,
1720 kRootDomainNoPublishSettingsCount,
1721 0);
1722
1723 fPMSettingsDict = OSDictionary::withCapacity(5);
1724 preventIdleSleepList = OSSet::withCapacity(8);
1725 preventSystemSleepList = OSSet::withCapacity(2);
1726
1727 PMinit(); // creates gIOPMWorkLoop
1728 gIOPMWorkLoop = getIOPMWorkloop();
1729
1730 // Create IOPMPowerStateQueue used to queue external power
1731 // events, and to handle those events on the PM work loop.
1732 pmPowerStateQueue = IOPMPowerStateQueue::PMPowerStateQueue(
1733 this, OSMemberFunctionCast(IOEventSource::Action, this,
1734 &IOPMrootDomain::dispatchPowerEvent));
1735 gIOPMWorkLoop->addEventSource(pmPowerStateQueue);
1736
1737 _aotMode = 0;
1738 _aotTimerES = IOTimerEventSource::timerEventSource(this,
1739 OSMemberFunctionCast(IOTimerEventSource::Action,
1740 this, &IOPMrootDomain::aotEvaluate));
1741 gIOPMWorkLoop->addEventSource(_aotTimerES.get());
1742
1743 // Avoid publishing service early so gIOPMWorkLoop is
1744 // guaranteed to be initialized by rootDomain.
1745 setPMRootDomain(this);
1746
1747 // create our power parent
1748 gPatriarch = new IORootParent;
1749 gPatriarch->init();
1750 gPatriarch->attach(this);
1751 gPatriarch->start(this);
1752 gPatriarch->addPowerChild(this);
1753
1754 registerPowerDriver(this, ourPowerStates, NUM_POWER_STATES);
1755 changePowerStateWithTagToPriv(ON_STATE, kCPSReasonInit);
1756
1757 // install power change handler
1758 gSysPowerDownNotifier = registerPrioritySleepWakeInterest( &sysPowerDownHandler, this, NULL);
1759
1760 #if DISPLAY_WRANGLER_PRESENT
1761 wranglerIdleSettings = OSDictionary::withCapacity(1);
1762 OSSharedPtr<OSNumber> wranglerIdlePeriod = OSNumber::withNumber(kDefaultWranglerIdlePeriod, 32);
1763
1764 if (wranglerIdleSettings && wranglerIdlePeriod) {
1765 wranglerIdleSettings->setObject(kIORequestWranglerIdleKey,
1766 wranglerIdlePeriod.get());
1767 }
1768
1769 #endif /* DISPLAY_WRANGLER_PRESENT */
1770
1771 lowLatencyAudioNotifierDict = OSDictionary::withCapacity(2);
1772 lowLatencyAudioNotifyStateSym = OSSymbol::withCString("LowLatencyAudioNotifyState");
1773 lowLatencyAudioNotifyTimestampSym = OSSymbol::withCString("LowLatencyAudioNotifyTimestamp");
1774 lowLatencyAudioNotifyStateVal = OSNumber::withNumber(0ull, 32);
1775 lowLatencyAudioNotifyTimestampVal = OSNumber::withNumber(0ull, 64);
1776
1777 if (lowLatencyAudioNotifierDict && lowLatencyAudioNotifyStateSym && lowLatencyAudioNotifyTimestampSym &&
1778 lowLatencyAudioNotifyStateVal && lowLatencyAudioNotifyTimestampVal) {
1779 lowLatencyAudioNotifierDict->setObject(lowLatencyAudioNotifyStateSym.get(), lowLatencyAudioNotifyStateVal.get());
1780 lowLatencyAudioNotifierDict->setObject(lowLatencyAudioNotifyTimestampSym.get(), lowLatencyAudioNotifyTimestampVal.get());
1781 }
1782
1783 OSSharedPtr<const OSSymbol> ucClassName = OSSymbol::withCStringNoCopy("RootDomainUserClient");
1784 setProperty(gIOUserClientClassKey, const_cast<OSObject *>(static_cast<const OSObject *>(ucClassName.get())));
1785
1786 // IOBacklightDisplay can take a long time to load at boot, or it may
1787 // not load at all if you're booting with clamshell closed. We publish
1788 // 'DisplayDims' here redundantly to get it published early and at all.
1789 OSSharedPtr<OSDictionary> matching;
1790 matching = serviceMatching("IOPMPowerSource");
1791 psIterator = getMatchingServices(matching.get());
1792
1793 if (psIterator && psIterator->getNextObject()) {
1794 // There's at least one battery on the system, so we publish
1795 // 'DisplayDims' support for the LCD.
1796 publishFeature("DisplayDims");
1797 }
1798
1799 // read swd_panic boot-arg
1800 PE_parse_boot_argn("swd_panic", &gSwdPanic, sizeof(gSwdPanic));
1801 gWillShutdownSysctlRegistered = true;
1802
1803 #if HIBERNATION
1804 #if defined(__arm64__)
1805 #endif /* defined(__arm64__) */
1806 IOHibernateSystemInit(this);
1807 #endif
1808
1809 registerService(); // let clients find us
1810
1811 return true;
1812 }
1813
1814 //******************************************************************************
1815 // setProperties
1816 //
1817 // Receive a setProperty call
1818 // The "System Boot" property means the system is completely booted.
1819 //******************************************************************************
1820
1821 IOReturn
setProperties(OSObject * props_obj)1822 IOPMrootDomain::setProperties( OSObject * props_obj )
1823 {
1824 IOReturn return_value = kIOReturnSuccess;
1825 OSDictionary *dict = OSDynamicCast(OSDictionary, props_obj);
1826 OSBoolean *b = NULL;
1827 OSNumber *n = NULL;
1828 const OSSymbol *key = NULL;
1829 OSObject *obj = NULL;
1830 OSSharedPtr<OSCollectionIterator> iter;
1831
1832 if (!dict) {
1833 return kIOReturnBadArgument;
1834 }
1835
1836 bool clientEntitled = false;
1837 {
1838 OSSharedPtr<OSObject> obj = IOUserClient::copyClientEntitlement(current_task(), kRootDomainEntitlementSetProperty);
1839 clientEntitled = (obj == kOSBooleanTrue);
1840 }
1841
1842 if (!clientEntitled) {
1843 const char * errorSuffix = NULL;
1844
1845 // IOPMSchedulePowerEvent() clients may not be entitled, but must be root.
1846 // That API can set 6 possible keys that are checked below.
1847 if ((dict->getCount() == 1) &&
1848 (dict->getObject(gIOPMSettingAutoWakeSecondsKey.get()) ||
1849 dict->getObject(gIOPMSettingAutoPowerSecondsKey.get()) ||
1850 dict->getObject(gIOPMSettingAutoWakeCalendarKey.get()) ||
1851 dict->getObject(gIOPMSettingAutoPowerCalendarKey.get()) ||
1852 dict->getObject(gIOPMSettingDebugWakeRelativeKey.get()) ||
1853 dict->getObject(gIOPMSettingDebugPowerRelativeKey.get()))) {
1854 return_value = IOUserClient::clientHasPrivilege(current_task(), kIOClientPrivilegeAdministrator);
1855 if (return_value != kIOReturnSuccess) {
1856 errorSuffix = "privileged";
1857 }
1858 } else {
1859 return_value = kIOReturnNotPermitted;
1860 errorSuffix = "entitled";
1861 }
1862
1863 if (return_value != kIOReturnSuccess) {
1864 OSSharedPtr<OSString> procName(IOCopyLogNameForPID(proc_selfpid()), OSNoRetain);
1865 DLOG("%s failed, process %s is not %s\n", __func__,
1866 procName ? procName->getCStringNoCopy() : "", errorSuffix);
1867 return return_value;
1868 }
1869 }
1870
1871 OSSharedPtr<const OSSymbol> publish_simulated_battery_string = OSSymbol::withCString("SoftwareSimulatedBatteries");
1872 OSSharedPtr<const OSSymbol> boot_complete_string = OSSymbol::withCString("System Boot Complete");
1873 OSSharedPtr<const OSSymbol> sys_shutdown_string = OSSymbol::withCString("System Shutdown");
1874 OSSharedPtr<const OSSymbol> stall_halt_string = OSSymbol::withCString("StallSystemAtHalt");
1875 OSSharedPtr<const OSSymbol> battery_warning_disabled_string = OSSymbol::withCString("BatteryWarningsDisabled");
1876 OSSharedPtr<const OSSymbol> idle_seconds_string = OSSymbol::withCString("System Idle Seconds");
1877 OSSharedPtr<const OSSymbol> idle_milliseconds_string = OSSymbol::withCString("System Idle Milliseconds");
1878 OSSharedPtr<const OSSymbol> sleepdisabled_string = OSSymbol::withCString("SleepDisabled");
1879 OSSharedPtr<const OSSymbol> ondeck_sleepwake_uuid_string = OSSymbol::withCString(kIOPMSleepWakeUUIDKey);
1880 OSSharedPtr<const OSSymbol> loginwindow_progress_string = OSSymbol::withCString(kIOPMLoginWindowProgressKey);
1881 OSSharedPtr<const OSSymbol> coredisplay_progress_string = OSSymbol::withCString(kIOPMCoreDisplayProgressKey);
1882 OSSharedPtr<const OSSymbol> coregraphics_progress_string = OSSymbol::withCString(kIOPMCoreGraphicsProgressKey);
1883 #if DEBUG || DEVELOPMENT
1884 OSSharedPtr<const OSSymbol> clamshell_close_string = OSSymbol::withCString("IOPMTestClamshellClose");
1885 OSSharedPtr<const OSSymbol> clamshell_open_string = OSSymbol::withCString("IOPMTestClamshellOpen");
1886 OSSharedPtr<const OSSymbol> ac_detach_string = OSSymbol::withCString("IOPMTestACDetach");
1887 OSSharedPtr<const OSSymbol> ac_attach_string = OSSymbol::withCString("IOPMTestACAttach");
1888 OSSharedPtr<const OSSymbol> desktopmode_set_string = OSSymbol::withCString("IOPMTestDesktopModeSet");
1889 OSSharedPtr<const OSSymbol> desktopmode_remove_string = OSSymbol::withCString("IOPMTestDesktopModeRemove");
1890 #endif
1891
1892 #if HIBERNATION
1893 OSSharedPtr<const OSSymbol> hibernatemode_string = OSSymbol::withCString(kIOHibernateModeKey);
1894 OSSharedPtr<const OSSymbol> hibernatefile_string = OSSymbol::withCString(kIOHibernateFileKey);
1895 OSSharedPtr<const OSSymbol> hibernatefilemin_string = OSSymbol::withCString(kIOHibernateFileMinSizeKey);
1896 OSSharedPtr<const OSSymbol> hibernatefilemax_string = OSSymbol::withCString(kIOHibernateFileMaxSizeKey);
1897 OSSharedPtr<const OSSymbol> hibernatefreeratio_string = OSSymbol::withCString(kIOHibernateFreeRatioKey);
1898 OSSharedPtr<const OSSymbol> hibernatefreetime_string = OSSymbol::withCString(kIOHibernateFreeTimeKey);
1899 #endif
1900
1901 iter = OSCollectionIterator::withCollection(dict);
1902 if (!iter) {
1903 return_value = kIOReturnNoMemory;
1904 goto exit;
1905 }
1906
1907 while ((key = (const OSSymbol *) iter->getNextObject()) &&
1908 (obj = dict->getObject(key))) {
1909 if (key->isEqualTo(publish_simulated_battery_string.get())) {
1910 if (OSDynamicCast(OSBoolean, obj)) {
1911 publishResource(key, kOSBooleanTrue);
1912 }
1913 } else if (key->isEqualTo(idle_seconds_string.get())) {
1914 if ((n = OSDynamicCast(OSNumber, obj))) {
1915 setProperty(key, n);
1916 idleMilliSeconds = n->unsigned32BitValue() * 1000;
1917 }
1918 } else if (key->isEqualTo(idle_milliseconds_string.get())) {
1919 if ((n = OSDynamicCast(OSNumber, obj))) {
1920 setProperty(key, n);
1921 idleMilliSeconds = n->unsigned32BitValue();
1922 }
1923 } else if (key->isEqualTo(boot_complete_string.get())) {
1924 pmPowerStateQueue->submitPowerEvent(kPowerEventSystemBootCompleted);
1925 } else if (key->isEqualTo(sys_shutdown_string.get())) {
1926 if ((b = OSDynamicCast(OSBoolean, obj))) {
1927 pmPowerStateQueue->submitPowerEvent(kPowerEventSystemShutdown, (void *) b);
1928 }
1929 } else if (key->isEqualTo(battery_warning_disabled_string.get())) {
1930 setProperty(key, obj);
1931 }
1932 #if HIBERNATION
1933 else if (key->isEqualTo(hibernatemode_string.get()) ||
1934 key->isEqualTo(hibernatefilemin_string.get()) ||
1935 key->isEqualTo(hibernatefilemax_string.get()) ||
1936 key->isEqualTo(hibernatefreeratio_string.get()) ||
1937 key->isEqualTo(hibernatefreetime_string.get())) {
1938 if ((n = OSDynamicCast(OSNumber, obj))) {
1939 setProperty(key, n);
1940 }
1941 } else if (key->isEqualTo(hibernatefile_string.get())) {
1942 OSString * str = OSDynamicCast(OSString, obj);
1943 if (str) {
1944 setProperty(key, str);
1945 }
1946 }
1947 #endif
1948 else if (key->isEqualTo(sleepdisabled_string.get())) {
1949 if ((b = OSDynamicCast(OSBoolean, obj))) {
1950 setProperty(key, b);
1951 pmPowerStateQueue->submitPowerEvent(kPowerEventUserDisabledSleep, (void *) b);
1952 }
1953 } else if (key->isEqualTo(ondeck_sleepwake_uuid_string.get())) {
1954 obj->retain();
1955 pmPowerStateQueue->submitPowerEvent(kPowerEventQueueSleepWakeUUID, (void *)obj);
1956 } else if (key->isEqualTo(loginwindow_progress_string.get())) {
1957 if (pmTracer && (n = OSDynamicCast(OSNumber, obj))) {
1958 uint32_t data = n->unsigned32BitValue();
1959 pmTracer->traceComponentWakeProgress(kIOPMLoginWindowProgress, data);
1960 kdebugTrace(kPMLogComponentWakeProgress, 0, kIOPMLoginWindowProgress, data);
1961 }
1962 } else if (key->isEqualTo(coredisplay_progress_string.get())) {
1963 if (pmTracer && (n = OSDynamicCast(OSNumber, obj))) {
1964 uint32_t data = n->unsigned32BitValue();
1965 pmTracer->traceComponentWakeProgress(kIOPMCoreDisplayProgress, data);
1966 kdebugTrace(kPMLogComponentWakeProgress, 0, kIOPMCoreDisplayProgress, data);
1967 }
1968 } else if (key->isEqualTo(coregraphics_progress_string.get())) {
1969 if (pmTracer && (n = OSDynamicCast(OSNumber, obj))) {
1970 uint32_t data = n->unsigned32BitValue();
1971 pmTracer->traceComponentWakeProgress(kIOPMCoreGraphicsProgress, data);
1972 kdebugTrace(kPMLogComponentWakeProgress, 0, kIOPMCoreGraphicsProgress, data);
1973 }
1974 } else if (key->isEqualTo(kIOPMDeepSleepEnabledKey) ||
1975 key->isEqualTo(kIOPMDestroyFVKeyOnStandbyKey) ||
1976 key->isEqualTo(kIOPMAutoPowerOffEnabledKey) ||
1977 key->isEqualTo(stall_halt_string.get())) {
1978 if ((b = OSDynamicCast(OSBoolean, obj))) {
1979 setProperty(key, b);
1980 }
1981 } else if (key->isEqualTo(kIOPMDeepSleepDelayKey) ||
1982 key->isEqualTo(kIOPMDeepSleepTimerKey) ||
1983 key->isEqualTo(kIOPMAutoPowerOffDelayKey) ||
1984 key->isEqualTo(kIOPMAutoPowerOffTimerKey)) {
1985 if ((n = OSDynamicCast(OSNumber, obj))) {
1986 setProperty(key, n);
1987 }
1988 } else if (key->isEqualTo(kIOPMUserWakeAlarmScheduledKey)) {
1989 if (kOSBooleanTrue == obj) {
1990 OSBitOrAtomic(kIOPMAlarmBitCalendarWake, &_userScheduledAlarmMask);
1991 } else {
1992 OSBitAndAtomic(~kIOPMAlarmBitCalendarWake, &_userScheduledAlarmMask);
1993 }
1994 DLOG("_userScheduledAlarmMask 0x%x\n", (uint32_t) _userScheduledAlarmMask);
1995 }
1996 #if DEBUG || DEVELOPMENT
1997 else if (key->isEqualTo(clamshell_close_string.get())) {
1998 DLOG("SetProperties: setting clamshell close\n");
1999 UInt32 msg = kIOPMClamshellClosed;
2000 pmPowerStateQueue->submitPowerEvent(kPowerEventReceivedPowerNotification, (void *)(uintptr_t)msg);
2001 } else if (key->isEqualTo(clamshell_open_string.get())) {
2002 DLOG("SetProperties: setting clamshell open\n");
2003 UInt32 msg = kIOPMClamshellOpened;
2004 pmPowerStateQueue->submitPowerEvent(kPowerEventReceivedPowerNotification, (void *)(uintptr_t)msg);
2005 } else if (key->isEqualTo(ac_detach_string.get())) {
2006 DLOG("SetProperties: setting ac detach\n");
2007 UInt32 msg = kIOPMSetACAdaptorConnected;
2008 pmPowerStateQueue->submitPowerEvent(kPowerEventReceivedPowerNotification, (void *)(uintptr_t)msg);
2009 } else if (key->isEqualTo(ac_attach_string.get())) {
2010 DLOG("SetProperties: setting ac attach\n");
2011 UInt32 msg = kIOPMSetACAdaptorConnected | kIOPMSetValue;
2012 pmPowerStateQueue->submitPowerEvent(kPowerEventReceivedPowerNotification, (void *)(uintptr_t)msg);
2013 } else if (key->isEqualTo(desktopmode_set_string.get())) {
2014 DLOG("SetProperties: setting desktopmode");
2015 UInt32 msg = kIOPMSetDesktopMode | kIOPMSetValue;
2016 pmPowerStateQueue->submitPowerEvent(kPowerEventReceivedPowerNotification, (void *)(uintptr_t)msg);
2017 } else if (key->isEqualTo(desktopmode_remove_string.get())) {
2018 DLOG("SetProperties: removing desktopmode\n");
2019 UInt32 msg = kIOPMSetDesktopMode;
2020 pmPowerStateQueue->submitPowerEvent(kPowerEventReceivedPowerNotification, (void *)(uintptr_t)msg);
2021 }
2022 #endif
2023 // Relay our allowed PM settings onto our registered PM clients
2024 else if ((allowedPMSettings->getNextIndexOfObject(key, 0) != (unsigned int) -1)) {
2025 return_value = setPMSetting(key, obj);
2026 if (kIOReturnSuccess != return_value) {
2027 break;
2028 }
2029 } else {
2030 DLOG("setProperties(%s) not handled\n", key->getCStringNoCopy());
2031 }
2032 }
2033
2034 exit:
2035 return return_value;
2036 }
2037
2038 // MARK: -
2039 // MARK: Aggressiveness
2040
2041 //******************************************************************************
2042 // setAggressiveness
2043 //
2044 // Override IOService::setAggressiveness()
2045 //******************************************************************************
2046
2047 IOReturn
setAggressiveness(unsigned long type,unsigned long value)2048 IOPMrootDomain::setAggressiveness(
2049 unsigned long type,
2050 unsigned long value )
2051 {
2052 return setAggressiveness( type, value, 0 );
2053 }
2054
2055 /*
2056 * Private setAggressiveness() with an internal options argument.
2057 */
2058 IOReturn
setAggressiveness(unsigned long type,unsigned long value,IOOptionBits options)2059 IOPMrootDomain::setAggressiveness(
2060 unsigned long type,
2061 unsigned long value,
2062 IOOptionBits options )
2063 {
2064 AggressivesRequest * entry;
2065 AggressivesRequest * request;
2066 bool found = false;
2067
2068 if ((type > UINT_MAX) || (value > UINT_MAX)) {
2069 return kIOReturnBadArgument;
2070 }
2071
2072 if (type == kPMMinutesToDim || type == kPMMinutesToSleep) {
2073 DLOG("setAggressiveness(%x) %s = %u\n",
2074 (uint32_t) options, getAggressivenessTypeString((uint32_t) type), (uint32_t) value);
2075 } else {
2076 DEBUG_LOG("setAggressiveness(%x) %s = %u\n",
2077 (uint32_t) options, getAggressivenessTypeString((uint32_t) type), (uint32_t) value);
2078 }
2079
2080 request = IOMallocType(AggressivesRequest);
2081 request->options = options;
2082 request->dataType = kAggressivesRequestTypeRecord;
2083 request->data.record.type = (uint32_t) type;
2084 request->data.record.value = (uint32_t) value;
2085
2086 AGGRESSIVES_LOCK();
2087
2088 // Update disk quick spindown flag used by getAggressiveness().
2089 // Never merge requests with quick spindown flags set.
2090
2091 if (options & kAggressivesOptionQuickSpindownEnable) {
2092 gAggressivesState |= kAggressivesStateQuickSpindown;
2093 } else if (options & kAggressivesOptionQuickSpindownDisable) {
2094 gAggressivesState &= ~kAggressivesStateQuickSpindown;
2095 } else {
2096 // Coalesce requests with identical aggressives types.
2097 // Deal with callers that calls us too "aggressively".
2098
2099 queue_iterate(&aggressivesQueue, entry, AggressivesRequest *, chain)
2100 {
2101 if ((entry->dataType == kAggressivesRequestTypeRecord) &&
2102 (entry->data.record.type == type) &&
2103 ((entry->options & kAggressivesOptionQuickSpindownMask) == 0)) {
2104 entry->data.record.value = (uint32_t) value;
2105 found = true;
2106 break;
2107 }
2108 }
2109 }
2110
2111 if (!found) {
2112 queue_enter(&aggressivesQueue, request, AggressivesRequest *, chain);
2113 }
2114
2115 AGGRESSIVES_UNLOCK();
2116
2117 if (found) {
2118 IOFreeType(request, AggressivesRequest);
2119 }
2120
2121 if (options & kAggressivesOptionSynchronous) {
2122 handleAggressivesRequests(); // not truly synchronous
2123 } else {
2124 thread_call_enter(aggressivesThreadCall);
2125 }
2126
2127 return kIOReturnSuccess;
2128 }
2129
2130 //******************************************************************************
2131 // getAggressiveness
2132 //
2133 // Override IOService::setAggressiveness()
2134 // Fetch the aggressiveness factor with the given type.
2135 //******************************************************************************
2136
2137 IOReturn
getAggressiveness(unsigned long type,unsigned long * outLevel)2138 IOPMrootDomain::getAggressiveness(
2139 unsigned long type,
2140 unsigned long * outLevel )
2141 {
2142 uint32_t value = 0;
2143 int source = 0;
2144
2145 if (!outLevel || (type > UINT_MAX)) {
2146 return kIOReturnBadArgument;
2147 }
2148
2149 AGGRESSIVES_LOCK();
2150
2151 // Disk quick spindown in effect, report value = 1
2152
2153 if ((gAggressivesState & kAggressivesStateQuickSpindown) &&
2154 (type == kPMMinutesToSpinDown)) {
2155 value = kAggressivesMinValue;
2156 source = 1;
2157 }
2158
2159 // Consult the pending request queue.
2160
2161 if (!source) {
2162 AggressivesRequest * entry;
2163
2164 queue_iterate(&aggressivesQueue, entry, AggressivesRequest *, chain)
2165 {
2166 if ((entry->dataType == kAggressivesRequestTypeRecord) &&
2167 (entry->data.record.type == type) &&
2168 ((entry->options & kAggressivesOptionQuickSpindownMask) == 0)) {
2169 value = entry->data.record.value;
2170 source = 2;
2171 break;
2172 }
2173 }
2174 }
2175
2176 // Consult the backend records.
2177
2178 if (!source && aggressivesData) {
2179 AggressivesRecord * record;
2180 int i, count;
2181
2182 count = aggressivesData->getLength() / sizeof(AggressivesRecord);
2183 record = (AggressivesRecord *) aggressivesData->getBytesNoCopy();
2184
2185 for (i = 0; i < count; i++, record++) {
2186 if (record->type == type) {
2187 value = record->value;
2188 source = 3;
2189 break;
2190 }
2191 }
2192 }
2193
2194 AGGRESSIVES_UNLOCK();
2195
2196 if (source) {
2197 *outLevel = (unsigned long) value;
2198 return kIOReturnSuccess;
2199 } else {
2200 DLOG("getAggressiveness type 0x%x not found\n", (uint32_t) type);
2201 *outLevel = 0; // default return = 0, driver may not check for error
2202 return kIOReturnInvalid;
2203 }
2204 }
2205
2206 //******************************************************************************
2207 // joinAggressiveness
2208 //
2209 // Request from IOService to join future aggressiveness broadcasts.
2210 //******************************************************************************
2211
2212 IOReturn
joinAggressiveness(IOService * service)2213 IOPMrootDomain::joinAggressiveness(
2214 IOService * service )
2215 {
2216 AggressivesRequest * request;
2217
2218 if (!service || (service == this)) {
2219 return kIOReturnBadArgument;
2220 }
2221
2222 DEBUG_LOG("joinAggressiveness %s %p\n", service->getName(), OBFUSCATE(service));
2223
2224 request = IOMallocType(AggressivesRequest);
2225 request->dataType = kAggressivesRequestTypeService;
2226 request->data.service.reset(service, OSRetain); // released by synchronizeAggressives()
2227
2228 AGGRESSIVES_LOCK();
2229 queue_enter(&aggressivesQueue, request, AggressivesRequest *, chain);
2230 AGGRESSIVES_UNLOCK();
2231
2232 thread_call_enter(aggressivesThreadCall);
2233
2234 return kIOReturnSuccess;
2235 }
2236
2237 //******************************************************************************
2238 // handleAggressivesRequests
2239 //
2240 // Backend thread processes all incoming aggressiveness requests in the queue.
2241 //******************************************************************************
2242
2243 static void
handleAggressivesFunction(thread_call_param_t param1,thread_call_param_t param2)2244 handleAggressivesFunction(
2245 thread_call_param_t param1,
2246 thread_call_param_t param2 )
2247 {
2248 if (param1) {
2249 ((IOPMrootDomain *) param1)->handleAggressivesRequests();
2250 }
2251 }
2252
2253 void
handleAggressivesRequests(void)2254 IOPMrootDomain::handleAggressivesRequests( void )
2255 {
2256 AggressivesRecord * start;
2257 AggressivesRecord * record;
2258 AggressivesRequest * request;
2259 queue_head_t joinedQueue;
2260 int i, count;
2261 bool broadcast;
2262 bool found;
2263 bool pingSelf = false;
2264
2265 AGGRESSIVES_LOCK();
2266
2267 if ((gAggressivesState & kAggressivesStateBusy) || !aggressivesData ||
2268 queue_empty(&aggressivesQueue)) {
2269 goto unlock_done;
2270 }
2271
2272 gAggressivesState |= kAggressivesStateBusy;
2273 count = aggressivesData->getLength() / sizeof(AggressivesRecord);
2274 start = (AggressivesRecord *) aggressivesData->getBytesNoCopy();
2275
2276 do{
2277 broadcast = false;
2278 queue_init(&joinedQueue);
2279
2280 do{
2281 // Remove request from the incoming queue in FIFO order.
2282 queue_remove_first(&aggressivesQueue, request, AggressivesRequest *, chain);
2283 switch (request->dataType) {
2284 case kAggressivesRequestTypeRecord:
2285 // Update existing record if found.
2286 found = false;
2287 for (i = 0, record = start; i < count; i++, record++) {
2288 if (record->type == request->data.record.type) {
2289 found = true;
2290
2291 if (request->options & kAggressivesOptionQuickSpindownEnable) {
2292 if ((record->flags & kAggressivesRecordFlagMinValue) == 0) {
2293 broadcast = true;
2294 record->flags |= (kAggressivesRecordFlagMinValue |
2295 kAggressivesRecordFlagModified);
2296 DLOG("disk spindown accelerated, was %u min\n",
2297 record->value);
2298 }
2299 } else if (request->options & kAggressivesOptionQuickSpindownDisable) {
2300 if (record->flags & kAggressivesRecordFlagMinValue) {
2301 broadcast = true;
2302 record->flags |= kAggressivesRecordFlagModified;
2303 record->flags &= ~kAggressivesRecordFlagMinValue;
2304 DLOG("disk spindown restored to %u min\n",
2305 record->value);
2306 }
2307 } else if (record->value != request->data.record.value) {
2308 record->value = request->data.record.value;
2309 if ((record->flags & kAggressivesRecordFlagMinValue) == 0) {
2310 broadcast = true;
2311 record->flags |= kAggressivesRecordFlagModified;
2312 }
2313 }
2314 break;
2315 }
2316 }
2317
2318 // No matching record, append a new record.
2319 if (!found &&
2320 ((request->options & kAggressivesOptionQuickSpindownDisable) == 0)) {
2321 AggressivesRecord newRecord;
2322
2323 newRecord.flags = kAggressivesRecordFlagModified;
2324 newRecord.type = request->data.record.type;
2325 newRecord.value = request->data.record.value;
2326 if (request->options & kAggressivesOptionQuickSpindownEnable) {
2327 newRecord.flags |= kAggressivesRecordFlagMinValue;
2328 DLOG("disk spindown accelerated\n");
2329 }
2330
2331 aggressivesData->appendValue(newRecord);
2332
2333 // OSData may have switched to another (larger) buffer.
2334 count = aggressivesData->getLength() / sizeof(AggressivesRecord);
2335 start = (AggressivesRecord *) aggressivesData->getBytesNoCopy();
2336 broadcast = true;
2337 }
2338
2339 // Finished processing the request, release it.
2340 IOFreeType(request, AggressivesRequest);
2341 break;
2342
2343 case kAggressivesRequestTypeService:
2344 // synchronizeAggressives() will free request.
2345 queue_enter(&joinedQueue, request, AggressivesRequest *, chain);
2346 break;
2347
2348 default:
2349 panic("bad aggressives request type %x", request->dataType);
2350 break;
2351 }
2352 } while (!queue_empty(&aggressivesQueue));
2353
2354 // Release the lock to perform work, with busy flag set.
2355 if (!queue_empty(&joinedQueue) || broadcast) {
2356 AGGRESSIVES_UNLOCK();
2357 if (!queue_empty(&joinedQueue)) {
2358 synchronizeAggressives(&joinedQueue, start, count);
2359 }
2360 if (broadcast) {
2361 broadcastAggressives(start, count);
2362 }
2363 AGGRESSIVES_LOCK();
2364 }
2365
2366 // Remove the modified flag from all records.
2367 for (i = 0, record = start; i < count; i++, record++) {
2368 if ((record->flags & kAggressivesRecordFlagModified) &&
2369 ((record->type == kPMMinutesToDim) ||
2370 (record->type == kPMMinutesToSleep))) {
2371 pingSelf = true;
2372 }
2373
2374 record->flags &= ~kAggressivesRecordFlagModified;
2375 }
2376
2377 // Check the incoming queue again since new entries may have been
2378 // added while lock was released above.
2379 } while (!queue_empty(&aggressivesQueue));
2380
2381 gAggressivesState &= ~kAggressivesStateBusy;
2382
2383 unlock_done:
2384 AGGRESSIVES_UNLOCK();
2385
2386 // Root domain is interested in system and display sleep slider changes.
2387 // Submit a power event to handle those changes on the PM work loop.
2388
2389 if (pingSelf && pmPowerStateQueue) {
2390 pmPowerStateQueue->submitPowerEvent(
2391 kPowerEventPolicyStimulus,
2392 (void *) kStimulusAggressivenessChanged );
2393 }
2394 }
2395
2396 //******************************************************************************
2397 // synchronizeAggressives
2398 //
2399 // Push all known aggressiveness records to one or more IOService.
2400 //******************************************************************************
2401
2402 void
synchronizeAggressives(queue_head_t * joinedQueue,const AggressivesRecord * array,int count)2403 IOPMrootDomain::synchronizeAggressives(
2404 queue_head_t * joinedQueue,
2405 const AggressivesRecord * array,
2406 int count )
2407 {
2408 OSSharedPtr<IOService> service;
2409 AggressivesRequest * request;
2410 const AggressivesRecord * record;
2411 IOPMDriverCallEntry callEntry;
2412 uint32_t value;
2413 int i;
2414
2415 while (!queue_empty(joinedQueue)) {
2416 queue_remove_first(joinedQueue, request, AggressivesRequest *, chain);
2417 if (request->dataType == kAggressivesRequestTypeService) {
2418 // retained by joinAggressiveness(), so take ownership
2419 service = os::move(request->data.service);
2420 } else {
2421 service.reset();
2422 }
2423
2424 IOFreeType(request, AggressivesRequest);
2425 request = NULL;
2426
2427 if (service) {
2428 if (service->assertPMDriverCall(&callEntry, kIOPMDriverCallMethodSetAggressive)) {
2429 for (i = 0, record = array; i < count; i++, record++) {
2430 value = record->value;
2431 if (record->flags & kAggressivesRecordFlagMinValue) {
2432 value = kAggressivesMinValue;
2433 }
2434
2435 _LOG("synchronizeAggressives 0x%x = %u to %s\n",
2436 record->type, value, service->getName());
2437 service->setAggressiveness(record->type, value);
2438 }
2439 service->deassertPMDriverCall(&callEntry);
2440 }
2441 }
2442 }
2443 }
2444
2445 //******************************************************************************
2446 // broadcastAggressives
2447 //
2448 // Traverse PM tree and call setAggressiveness() for records that have changed.
2449 //******************************************************************************
2450
2451 void
broadcastAggressives(const AggressivesRecord * array,int count)2452 IOPMrootDomain::broadcastAggressives(
2453 const AggressivesRecord * array,
2454 int count )
2455 {
2456 OSSharedPtr<IORegistryIterator> iter;
2457 IORegistryEntry *entry;
2458 OSSharedPtr<IORegistryEntry> child;
2459 IOPowerConnection *connect;
2460 IOService *service;
2461 const AggressivesRecord *record;
2462 IOPMDriverCallEntry callEntry;
2463 uint32_t value;
2464 int i;
2465
2466 iter = IORegistryIterator::iterateOver(
2467 this, gIOPowerPlane, kIORegistryIterateRecursively);
2468 if (iter) {
2469 do{
2470 // !! reset the iterator
2471 iter->reset();
2472 while ((entry = iter->getNextObject())) {
2473 connect = OSDynamicCast(IOPowerConnection, entry);
2474 if (!connect || !connect->getReadyFlag()) {
2475 continue;
2476 }
2477
2478 child = connect->copyChildEntry(gIOPowerPlane);
2479 if (child) {
2480 if ((service = OSDynamicCast(IOService, child.get()))) {
2481 if (service->assertPMDriverCall(&callEntry, kIOPMDriverCallMethodSetAggressive)) {
2482 for (i = 0, record = array; i < count; i++, record++) {
2483 if (record->flags & kAggressivesRecordFlagModified) {
2484 value = record->value;
2485 if (record->flags & kAggressivesRecordFlagMinValue) {
2486 value = kAggressivesMinValue;
2487 }
2488 _LOG("broadcastAggressives %x = %u to %s\n",
2489 record->type, value, service->getName());
2490 service->setAggressiveness(record->type, value);
2491 }
2492 }
2493 service->deassertPMDriverCall(&callEntry);
2494 }
2495 }
2496 }
2497 }
2498 }while (!entry && !iter->isValid());
2499 }
2500 }
2501
2502 //*****************************************
2503 // stackshot on power button press
2504 // ***************************************
2505 static void
powerButtonDownCallout(thread_call_param_t us,thread_call_param_t)2506 powerButtonDownCallout(thread_call_param_t us, thread_call_param_t )
2507 {
2508 /* Power button pressed during wake
2509 * Take a stackshot
2510 */
2511 DEBUG_LOG("Powerbutton: down. Taking stackshot\n");
2512 ((IOPMrootDomain *)us)->takeStackshot(false);
2513 }
2514
2515 static void
powerButtonUpCallout(thread_call_param_t us,thread_call_param_t)2516 powerButtonUpCallout(thread_call_param_t us, thread_call_param_t)
2517 {
2518 /* Power button released.
2519 * Delete any stackshot data
2520 */
2521 DEBUG_LOG("PowerButton: up callout. Delete stackshot\n");
2522 ((IOPMrootDomain *)us)->deleteStackshot();
2523 }
2524 //*************************************************************************
2525 //
2526
2527 // MARK: -
2528 // MARK: System Sleep
2529
2530 //******************************************************************************
2531 // startIdleSleepTimer
2532 //
2533 //******************************************************************************
2534
2535 void
startIdleSleepTimer(uint32_t inMilliSeconds)2536 IOPMrootDomain::startIdleSleepTimer( uint32_t inMilliSeconds )
2537 {
2538 AbsoluteTime deadline;
2539
2540 ASSERT_GATED();
2541 if (gNoIdleFlag) {
2542 DLOG("idle timer not set (noidle=%d)\n", gNoIdleFlag);
2543 return;
2544 }
2545 if (inMilliSeconds) {
2546 if (inMilliSeconds < kMinimumTimeBeforeIdleSleep) {
2547 AbsoluteTime now;
2548 uint64_t nsec_since_wake;
2549 uint64_t msec_since_wake;
2550
2551 // Adjust idle timer so it will not expire until atleast kMinimumTimeBeforeIdleSleep milliseconds
2552 // after the most recent AP wake.
2553 clock_get_uptime(&now);
2554 SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
2555 absolutetime_to_nanoseconds(now, &nsec_since_wake);
2556 msec_since_wake = nsec_since_wake / NSEC_PER_MSEC;
2557
2558 if (msec_since_wake < kMinimumTimeBeforeIdleSleep) {
2559 uint32_t newIdleTimer = kMinimumTimeBeforeIdleSleep - (uint32_t)msec_since_wake;
2560
2561 // Ensure that our new idle timer is not less than inMilliSeconds,
2562 // as we should only be increasing the timer duration, not decreasing it
2563 if (newIdleTimer > inMilliSeconds) {
2564 DLOG("startIdleSleepTimer increasing timeout from %u to %u\n", inMilliSeconds, newIdleTimer);
2565 inMilliSeconds = newIdleTimer;
2566 }
2567 }
2568 }
2569 clock_interval_to_deadline(inMilliSeconds, kMillisecondScale, &deadline);
2570 thread_call_enter_delayed(extraSleepTimer, deadline);
2571 idleSleepTimerPending = true;
2572 } else {
2573 thread_call_enter(extraSleepTimer);
2574 }
2575 DLOG("idle timer set for %u milliseconds\n", inMilliSeconds);
2576 }
2577
2578 //******************************************************************************
2579 // cancelIdleSleepTimer
2580 //
2581 //******************************************************************************
2582
2583 void
cancelIdleSleepTimer(void)2584 IOPMrootDomain::cancelIdleSleepTimer( void )
2585 {
2586 ASSERT_GATED();
2587 if (idleSleepTimerPending) {
2588 DLOG("idle timer cancelled\n");
2589 thread_call_cancel(extraSleepTimer);
2590 idleSleepTimerPending = false;
2591
2592 if (!assertOnWakeSecs && gIOLastWakeAbsTime) {
2593 AbsoluteTime now;
2594 clock_usec_t microsecs;
2595 clock_get_uptime(&now);
2596 SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
2597 absolutetime_to_microtime(now, &assertOnWakeSecs, µsecs);
2598 if (assertOnWakeReport) {
2599 HISTREPORT_TALLYVALUE(assertOnWakeReport, (int64_t)assertOnWakeSecs);
2600 DLOG("Updated assertOnWake %lu\n", (unsigned long)assertOnWakeSecs);
2601 }
2602 }
2603 }
2604 }
2605
2606 //******************************************************************************
2607 // idleSleepTimerExpired
2608 //
2609 //******************************************************************************
2610
2611 static void
idleSleepTimerExpired(thread_call_param_t us,thread_call_param_t)2612 idleSleepTimerExpired(
2613 thread_call_param_t us, thread_call_param_t )
2614 {
2615 ((IOPMrootDomain *)us)->handleSleepTimerExpiration();
2616 }
2617
2618 //******************************************************************************
2619 // handleSleepTimerExpiration
2620 //
2621 // The time between the sleep idle timeout and the next longest one has elapsed.
2622 // It's time to sleep. Start that by removing the clamp that's holding us awake.
2623 //******************************************************************************
2624
2625 void
handleSleepTimerExpiration(void)2626 IOPMrootDomain::handleSleepTimerExpiration( void )
2627 {
2628 if (!gIOPMWorkLoop->inGate()) {
2629 gIOPMWorkLoop->runAction(
2630 OSMemberFunctionCast(IOWorkLoop::Action, this,
2631 &IOPMrootDomain::handleSleepTimerExpiration),
2632 this);
2633 return;
2634 }
2635
2636 DLOG("sleep timer expired\n");
2637 ASSERT_GATED();
2638
2639 idleSleepTimerPending = false;
2640 setQuickSpinDownTimeout();
2641 adjustPowerState(true);
2642 }
2643
2644 //******************************************************************************
2645 // getTimeToIdleSleep
2646 //
2647 // Returns number of milliseconds left before going into idle sleep.
2648 // Caller has to make sure that idle sleep is allowed at the time of calling
2649 // this function
2650 //******************************************************************************
2651
2652 uint32_t
getTimeToIdleSleep(void)2653 IOPMrootDomain::getTimeToIdleSleep( void )
2654 {
2655 AbsoluteTime now, lastActivityTime;
2656 uint64_t nanos;
2657 uint32_t minutesSinceUserInactive = 0;
2658 uint32_t sleepDelay = 0;
2659
2660 if (!idleSleepEnabled) {
2661 return 0xffffffff;
2662 }
2663
2664 if (userActivityTime) {
2665 lastActivityTime = userActivityTime;
2666 } else {
2667 lastActivityTime = userBecameInactiveTime;
2668 }
2669
2670 // Ignore any lastActivityTime that predates the last system wake.
2671 // The goal is to avoid a sudden idle sleep right after a dark wake
2672 // due to sleepDelay=0 computed below. The alternative 60s minimum
2673 // timeout should be large enough to allow dark wake to complete,
2674 // at which point the idle timer will be promptly cancelled.
2675 clock_get_uptime(&now);
2676 if ((CMP_ABSOLUTETIME(&lastActivityTime, &gIOLastWakeAbsTime) >= 0) &&
2677 (CMP_ABSOLUTETIME(&now, &lastActivityTime) > 0)) {
2678 SUB_ABSOLUTETIME(&now, &lastActivityTime);
2679 absolutetime_to_nanoseconds(now, &nanos);
2680 minutesSinceUserInactive = nanos / (60000000000ULL);
2681
2682 if (minutesSinceUserInactive >= sleepSlider) {
2683 sleepDelay = 0;
2684 } else {
2685 sleepDelay = sleepSlider - minutesSinceUserInactive;
2686 }
2687 } else {
2688 DLOG("ignoring lastActivityTime 0x%qx, now 0x%qx, wake 0x%qx\n",
2689 lastActivityTime, now, gIOLastWakeAbsTime);
2690 sleepDelay = sleepSlider;
2691 }
2692
2693 DLOG("user inactive %u min, time to idle sleep %u min\n",
2694 minutesSinceUserInactive, sleepDelay);
2695
2696 return sleepDelay * 60 * 1000;
2697 }
2698
2699 //******************************************************************************
2700 // setQuickSpinDownTimeout
2701 //
2702 //******************************************************************************
2703
2704 void
setQuickSpinDownTimeout(void)2705 IOPMrootDomain::setQuickSpinDownTimeout( void )
2706 {
2707 ASSERT_GATED();
2708 setAggressiveness(
2709 kPMMinutesToSpinDown, 0, kAggressivesOptionQuickSpindownEnable );
2710 }
2711
2712 //******************************************************************************
2713 // restoreUserSpinDownTimeout
2714 //
2715 //******************************************************************************
2716
2717 void
restoreUserSpinDownTimeout(void)2718 IOPMrootDomain::restoreUserSpinDownTimeout( void )
2719 {
2720 ASSERT_GATED();
2721 setAggressiveness(
2722 kPMMinutesToSpinDown, 0, kAggressivesOptionQuickSpindownDisable );
2723 }
2724
2725 //******************************************************************************
2726 // sleepSystem
2727 //
2728 //******************************************************************************
2729
2730 /* public */
2731 IOReturn
sleepSystem(void)2732 IOPMrootDomain::sleepSystem( void )
2733 {
2734 return sleepSystemOptions(NULL);
2735 }
2736
2737 /* private */
2738 IOReturn
sleepSystemOptions(OSDictionary * options)2739 IOPMrootDomain::sleepSystemOptions( OSDictionary *options )
2740 {
2741 OSObject *obj = NULL;
2742 OSString *reason = NULL;
2743 /* sleepSystem is a public function, and may be called by any kernel driver.
2744 * And that's bad - drivers should sleep the system by calling
2745 * receivePowerNotification() instead. Drivers should not use sleepSystem.
2746 *
2747 * Note that user space app calls to IOPMSleepSystem() will also travel
2748 * this code path and thus be correctly identified as software sleeps.
2749 */
2750
2751 if (options && options->getObject("OSSwitch")) {
2752 // Log specific sleep cause for OS Switch hibernation
2753 return privateSleepSystem( kIOPMSleepReasonOSSwitchHibernate);
2754 }
2755
2756 if (options && (obj = options->getObject("Sleep Reason"))) {
2757 reason = OSDynamicCast(OSString, obj);
2758 if (reason && reason->isEqualTo(kIOPMDarkWakeThermalEmergencyKey)) {
2759 return privateSleepSystem(kIOPMSleepReasonDarkWakeThermalEmergency);
2760 }
2761 if (reason && reason->isEqualTo(kIOPMNotificationWakeExitKey)) {
2762 return privateSleepSystem(kIOPMSleepReasonNotificationWakeExit);
2763 }
2764 }
2765
2766 return privateSleepSystem( kIOPMSleepReasonSoftware);
2767 }
2768
2769 /* private */
2770 IOReturn
privateSleepSystem(uint32_t sleepReason)2771 IOPMrootDomain::privateSleepSystem( uint32_t sleepReason )
2772 {
2773 /* Called from both gated and non-gated context */
2774
2775 if (!checkSystemSleepEnabled() || !pmPowerStateQueue) {
2776 return kIOReturnNotPermitted;
2777 }
2778
2779 pmPowerStateQueue->submitPowerEvent(
2780 kPowerEventPolicyStimulus,
2781 (void *) kStimulusDemandSystemSleep,
2782 sleepReason);
2783
2784 return kIOReturnSuccess;
2785 }
2786
2787 //******************************************************************************
2788 // powerChangeDone
2789 //
2790 // This overrides powerChangeDone in IOService.
2791 //******************************************************************************
2792 void
powerChangeDone(unsigned long previousPowerState)2793 IOPMrootDomain::powerChangeDone( unsigned long previousPowerState )
2794 {
2795 #if !__i386__ && !__x86_64__
2796 uint64_t timeSinceReset = 0;
2797 #endif
2798 uint64_t now;
2799 unsigned long newState;
2800 clock_sec_t secs;
2801 clock_usec_t microsecs;
2802 uint32_t lastDebugWakeSeconds;
2803 clock_sec_t adjWakeTime;
2804 IOPMCalendarStruct nowCalendar;
2805
2806 ASSERT_GATED();
2807 newState = getPowerState();
2808 DLOG("PowerChangeDone: %s->%s\n",
2809 getPowerStateString((uint32_t) previousPowerState), getPowerStateString((uint32_t) getPowerState()));
2810
2811 if (previousPowerState == newState) {
2812 return;
2813 }
2814
2815 notifierThread = current_thread();
2816 switch (getPowerState()) {
2817 case SLEEP_STATE: {
2818 if (kPMCalendarTypeInvalid != _aotWakeTimeCalendar.selector) {
2819 secs = 0;
2820 microsecs = 0;
2821 PEGetUTCTimeOfDay(&secs, µsecs);
2822
2823 adjWakeTime = 0;
2824 if ((kIOPMAOTModeRespectTimers & _aotMode) && (_calendarWakeAlarmUTC < _aotWakeTimeUTC)) {
2825 IOLog("use _calendarWakeAlarmUTC\n");
2826 adjWakeTime = _calendarWakeAlarmUTC;
2827 } else if (kIOPMWakeEventAOTExitFlags & _aotPendingFlags) {
2828 IOLog("accelerate _aotWakeTime for exit\n");
2829 adjWakeTime = secs;
2830 } else if (kIOPMDriverAssertionLevelOn == getPMAssertionLevel(kIOPMDriverAssertionCPUBit)) {
2831 IOLog("accelerate _aotWakeTime for assertion\n");
2832 adjWakeTime = secs;
2833 }
2834 if (adjWakeTime) {
2835 IOPMConvertSecondsToCalendar(adjWakeTime, &_aotWakeTimeCalendar);
2836 }
2837
2838 IOPMConvertSecondsToCalendar(secs, &nowCalendar);
2839 IOLog("aotSleep at " YMDTF " sched: " YMDTF "\n", YMDT(&nowCalendar), YMDT(&_aotWakeTimeCalendar));
2840
2841 IOReturn __unused ret = setMaintenanceWakeCalendar(&_aotWakeTimeCalendar);
2842 assert(kIOReturnSuccess == ret);
2843 }
2844 if (_aotLastWakeTime) {
2845 _aotMetrics->totalTime += mach_absolute_time() - _aotLastWakeTime;
2846 if (_aotMetrics->sleepCount && (_aotMetrics->sleepCount <= kIOPMAOTMetricsKernelWakeCountMax)) {
2847 strlcpy(&_aotMetrics->kernelWakeReason[_aotMetrics->sleepCount - 1][0],
2848 gWakeReasonString,
2849 sizeof(_aotMetrics->kernelWakeReason[_aotMetrics->sleepCount]));
2850 }
2851 }
2852 _aotPendingFlags &= ~kIOPMWakeEventAOTPerCycleFlags;
2853 if (_aotTimerScheduled) {
2854 _aotTimerES->cancelTimeout();
2855 _aotTimerScheduled = false;
2856 }
2857 acceptSystemWakeEvents(kAcceptSystemWakeEvents_Enable);
2858
2859 // re-enable this timer for next sleep
2860 cancelIdleSleepTimer();
2861
2862 if (clamshellExists) {
2863 #if DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY
2864 if (gClamshellFlags & kClamshell_WAR_58009435) {
2865 // Disable clamshell sleep until system has completed full wake.
2866 // This prevents a system sleep request (due to a clamshell close)
2867 // from being queued until the end of system full wake - even if
2868 // other clamshell disable bits outside of our control is wrong.
2869 setClamShellSleepDisable(true, kClamshellSleepDisableInternal);
2870 }
2871 #endif
2872
2873 // Log the last known clamshell state before system sleep
2874 DLOG("clamshell closed %d, disabled %d/%x, desktopMode %d, ac %d\n",
2875 clamshellClosed, clamshellDisabled, clamshellSleepDisableMask,
2876 desktopMode, acAdaptorConnected);
2877 }
2878
2879 clock_get_calendar_absolute_and_microtime(&secs, µsecs, &now);
2880 logtime(secs);
2881 gIOLastSleepTime.tv_sec = secs;
2882 gIOLastSleepTime.tv_usec = microsecs;
2883 if (!_aotLastWakeTime) {
2884 gIOLastUserSleepTime = gIOLastSleepTime;
2885 }
2886
2887 gIOLastWakeTime.tv_sec = 0;
2888 gIOLastWakeTime.tv_usec = 0;
2889 gIOLastSleepAbsTime = now;
2890
2891 if (wake2DarkwakeDelay && sleepDelaysReport) {
2892 clock_sec_t wake2DarkwakeSecs, darkwake2SleepSecs;
2893 // Update 'wake2DarkwakeDelay' histogram if this is a fullwake->sleep transition
2894
2895 SUB_ABSOLUTETIME(&now, &ts_sleepStart);
2896 absolutetime_to_microtime(now, &darkwake2SleepSecs, µsecs);
2897 absolutetime_to_microtime(wake2DarkwakeDelay, &wake2DarkwakeSecs, µsecs);
2898 HISTREPORT_TALLYVALUE(sleepDelaysReport,
2899 (int64_t)(wake2DarkwakeSecs + darkwake2SleepSecs));
2900
2901 DLOG("Updated sleepDelaysReport %lu %lu\n", (unsigned long)wake2DarkwakeSecs, (unsigned long)darkwake2SleepSecs);
2902 wake2DarkwakeDelay = 0;
2903 }
2904 #if HIBERNATION
2905 LOG("System %sSleep\n", gIOHibernateState ? "Safe" : "");
2906
2907 IOHibernateSystemHasSlept();
2908
2909 evaluateSystemSleepPolicyFinal();
2910 #else
2911 LOG("System Sleep\n");
2912 #endif
2913 if (thermalWarningState) {
2914 OSSharedPtr<const OSSymbol> event = OSSymbol::withCString(kIOPMThermalLevelWarningKey);
2915 if (event) {
2916 systemPowerEventOccurred(event.get(), kIOPMThermalLevelUnknown);
2917 }
2918 }
2919 assertOnWakeSecs = 0;
2920 lowBatteryCondition = false;
2921 thermalEmergencyState = false;
2922
2923 #if DEVELOPMENT || DEBUG
2924 extern int g_should_log_clock_adjustments;
2925 if (g_should_log_clock_adjustments) {
2926 clock_sec_t secs = 0;
2927 clock_usec_t microsecs = 0;
2928 uint64_t now_b = mach_absolute_time();
2929
2930 secs = 0;
2931 microsecs = 0;
2932 PEGetUTCTimeOfDay(&secs, µsecs);
2933
2934 uint64_t now_a = mach_absolute_time();
2935 os_log(OS_LOG_DEFAULT, "%s PMU before going to sleep %lu s %d u %llu abs_b_PEG %llu abs_a_PEG \n",
2936 __func__, (unsigned long)secs, microsecs, now_b, now_a);
2937 }
2938 #endif
2939
2940 getPlatform()->sleepKernel();
2941
2942 // The CPU(s) are off at this point,
2943 // Code will resume execution here upon wake.
2944
2945 clock_get_uptime(&gIOLastWakeAbsTime);
2946 IOLog("gIOLastWakeAbsTime: %lld\n", gIOLastWakeAbsTime);
2947 _highestCapability = 0;
2948
2949 #if HIBERNATION
2950 IOHibernateSystemWake();
2951 #endif
2952
2953 // sleep transition complete
2954 gSleepOrShutdownPending = 0;
2955
2956 // trip the reset of the calendar clock
2957 clock_wakeup_calendar();
2958 clock_get_calendar_microtime(&secs, µsecs);
2959 gIOLastWakeTime.tv_sec = secs;
2960 gIOLastWakeTime.tv_usec = microsecs;
2961
2962 // aot
2963 if (_aotWakeTimeCalendar.selector != kPMCalendarTypeInvalid) {
2964 _aotWakeTimeCalendar.selector = kPMCalendarTypeInvalid;
2965 secs = 0;
2966 microsecs = 0;
2967 PEGetUTCTimeOfDay(&secs, µsecs);
2968 IOPMConvertSecondsToCalendar(secs, &nowCalendar);
2969 IOLog("aotWake at " YMDTF " sched: " YMDTF "\n", YMDT(&nowCalendar), YMDT(&_aotWakeTimeCalendar));
2970 _aotMetrics->sleepCount++;
2971 _aotLastWakeTime = gIOLastWakeAbsTime;
2972 if (_aotMetrics->sleepCount <= kIOPMAOTMetricsKernelWakeCountMax) {
2973 _aotMetrics->kernelSleepTime[_aotMetrics->sleepCount - 1]
2974 = (((uint64_t) gIOLastSleepTime.tv_sec) << 10) + (gIOLastSleepTime.tv_usec / 1000);
2975 _aotMetrics->kernelWakeTime[_aotMetrics->sleepCount - 1]
2976 = (((uint64_t) gIOLastWakeTime.tv_sec) << 10) + (gIOLastWakeTime.tv_usec / 1000);
2977 }
2978
2979 if (_aotTestTime) {
2980 if (_aotWakeTimeUTC <= secs) {
2981 _aotTestTime = _aotTestTime + _aotTestInterval;
2982 }
2983 setWakeTime(_aotTestTime);
2984 }
2985 }
2986
2987 #if HIBERNATION
2988 LOG("System %sWake\n", gIOHibernateState ? "SafeSleep " : "");
2989 #endif
2990
2991 lastSleepReason = 0;
2992
2993 lastDebugWakeSeconds = _debugWakeSeconds;
2994 _debugWakeSeconds = 0;
2995 _scheduledAlarmMask = 0;
2996 _nextScheduledAlarmType = NULL;
2997
2998 darkWakeExit = false;
2999 darkWakePowerClamped = false;
3000 darkWakePostTickle = false;
3001 darkWakeHibernateError = false;
3002 darkWakeToSleepASAP = true;
3003 darkWakeLogClamp = true;
3004 sleepTimerMaintenance = false;
3005 sleepToStandby = false;
3006 wranglerTickled = false;
3007 userWasActive = false;
3008 isRTCAlarmWake = false;
3009 clamshellIgnoreClose = false;
3010 fullWakeReason = kFullWakeReasonNone;
3011
3012 #if defined(__i386__) || defined(__x86_64__)
3013 kdebugTrace(kPMLogSystemWake, 0, 0, 0);
3014
3015 OSSharedPtr<OSObject> wakeTypeProp = copyProperty(kIOPMRootDomainWakeTypeKey);
3016 OSSharedPtr<OSObject> wakeReasonProp = copyProperty(kIOPMRootDomainWakeReasonKey);
3017 OSString * wakeType = OSDynamicCast(OSString, wakeTypeProp.get());
3018 OSString * wakeReason = OSDynamicCast(OSString, wakeReasonProp.get());
3019
3020 if (wakeReason && (wakeReason->getLength() >= 2) &&
3021 gWakeReasonString[0] == '\0') {
3022 WAKEEVENT_LOCK();
3023 // Until the platform driver can claim its wake reasons
3024 strlcat(gWakeReasonString, wakeReason->getCStringNoCopy(),
3025 sizeof(gWakeReasonString));
3026 if (!gWakeReasonSysctlRegistered) {
3027 gWakeReasonSysctlRegistered = true;
3028 }
3029 WAKEEVENT_UNLOCK();
3030 }
3031
3032 if (wakeType && wakeType->isEqualTo(kIOPMrootDomainWakeTypeLowBattery)) {
3033 lowBatteryCondition = true;
3034 darkWakeMaintenance = true;
3035 } else {
3036 #if HIBERNATION
3037 OSSharedPtr<OSObject> hibOptionsProp = copyProperty(kIOHibernateOptionsKey);
3038 OSNumber * hibOptions = OSDynamicCast( OSNumber, hibOptionsProp.get());
3039 if (hibernateAborted || ((hibOptions &&
3040 !(hibOptions->unsigned32BitValue() & kIOHibernateOptionDarkWake)))) {
3041 // Hibernate aborted, or EFI brought up graphics
3042 darkWakeExit = true;
3043 if (hibernateAborted) {
3044 DLOG("Hibernation aborted\n");
3045 } else {
3046 DLOG("EFI brought up graphics. Going to full wake. HibOptions: 0x%x\n", hibOptions->unsigned32BitValue());
3047 }
3048 } else
3049 #endif
3050 if (wakeType && (
3051 wakeType->isEqualTo(kIOPMRootDomainWakeTypeUser) ||
3052 wakeType->isEqualTo(kIOPMRootDomainWakeTypeAlarm))) {
3053 // User wake or RTC alarm
3054 darkWakeExit = true;
3055 if (wakeType->isEqualTo(kIOPMRootDomainWakeTypeAlarm)) {
3056 isRTCAlarmWake = true;
3057 }
3058 } else if (wakeType &&
3059 wakeType->isEqualTo(kIOPMRootDomainWakeTypeSleepTimer)) {
3060 // SMC standby timer trumps SleepX
3061 darkWakeMaintenance = true;
3062 sleepTimerMaintenance = true;
3063 } else if ((lastDebugWakeSeconds != 0) &&
3064 ((gDarkWakeFlags & kDarkWakeFlagAlarmIsDark) == 0)) {
3065 // SleepX before maintenance
3066 darkWakeExit = true;
3067 } else if (wakeType &&
3068 wakeType->isEqualTo(kIOPMRootDomainWakeTypeMaintenance)) {
3069 darkWakeMaintenance = true;
3070 } else if (wakeType &&
3071 wakeType->isEqualTo(kIOPMRootDomainWakeTypeSleepService)) {
3072 darkWakeMaintenance = true;
3073 darkWakeSleepService = true;
3074 #if HIBERNATION
3075 if (kIOHibernateStateWakingFromHibernate == gIOHibernateState) {
3076 sleepToStandby = true;
3077 }
3078 #endif
3079 } else if (wakeType &&
3080 wakeType->isEqualTo(kIOPMRootDomainWakeTypeHibernateError)) {
3081 darkWakeMaintenance = true;
3082 darkWakeHibernateError = true;
3083 } else {
3084 // Unidentified wake source, resume to full wake if debug
3085 // alarm is pending.
3086
3087 if (lastDebugWakeSeconds &&
3088 (!wakeReason || wakeReason->isEqualTo(""))) {
3089 darkWakeExit = true;
3090 }
3091 }
3092 }
3093
3094 if (darkWakeExit) {
3095 darkWakeToSleepASAP = false;
3096 fullWakeReason = kFullWakeReasonLocalUser;
3097 reportUserInput();
3098 } else if (displayPowerOnRequested && checkSystemCanSustainFullWake()) {
3099 handleSetDisplayPowerOn(true);
3100 } else if (!darkWakeMaintenance) {
3101 // Early/late tickle for non-maintenance wake.
3102 if ((gDarkWakeFlags & kDarkWakeFlagPromotionMask) != kDarkWakeFlagPromotionNone) {
3103 darkWakePostTickle = true;
3104 }
3105 }
3106 #else /* !__i386__ && !__x86_64__ */
3107 timeSinceReset = ml_get_time_since_reset();
3108 kdebugTrace(kPMLogSystemWake, 0, (uintptr_t)(timeSinceReset >> 32), (uintptr_t) timeSinceReset);
3109
3110 if ((gDarkWakeFlags & kDarkWakeFlagPromotionMask) == kDarkWakeFlagPromotionEarly) {
3111 wranglerTickled = true;
3112 fullWakeReason = kFullWakeReasonLocalUser;
3113 requestUserActive(this, "Full wake on dark wake promotion boot-arg");
3114 } else if ((lastDebugWakeSeconds != 0) && !(gDarkWakeFlags & kDarkWakeFlagAlarmIsDark)) {
3115 isRTCAlarmWake = true;
3116 fullWakeReason = kFullWakeReasonLocalUser;
3117 requestUserActive(this, "RTC debug alarm");
3118 } else {
3119 #if HIBERNATION
3120 OSSharedPtr<OSObject> hibOptionsProp = copyProperty(kIOHibernateOptionsKey);
3121 OSNumber * hibOptions = OSDynamicCast(OSNumber, hibOptionsProp.get());
3122 if (hibOptions && !(hibOptions->unsigned32BitValue() & kIOHibernateOptionDarkWake)) {
3123 fullWakeReason = kFullWakeReasonLocalUser;
3124 requestUserActive(this, "hibernate user wake");
3125 }
3126 #endif
3127 }
3128
3129 // stay awake for at least 30 seconds
3130 startIdleSleepTimer(30 * 1000);
3131 #endif
3132 sleepCnt++;
3133
3134 thread_call_enter(updateConsoleUsersEntry);
3135
3136 // Skip AOT_STATE if we are waking up from an RTC timer.
3137 // This check needs to be done after the epoch change is processed
3138 // and before the changePowerStateWithTagToPriv() call below.
3139 WAKEEVENT_LOCK();
3140 aotShouldExit(false, false);
3141 WAKEEVENT_UNLOCK();
3142
3143 changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonWake);
3144 break;
3145 }
3146 #if !__i386__ && !__x86_64__
3147 case ON_STATE:
3148 case AOT_STATE:
3149 {
3150 DLOG("Force re-evaluating aggressiveness\n");
3151 /* Force re-evaluate the aggressiveness values to set appropriate idle sleep timer */
3152 pmPowerStateQueue->submitPowerEvent(
3153 kPowerEventPolicyStimulus,
3154 (void *) kStimulusNoIdleSleepPreventers );
3155
3156 // After changing to ON_STATE, invalidate any previously queued
3157 // request to change to a state less than ON_STATE. This isn't
3158 // necessary for AOT_STATE or if the device has only one running
3159 // state since the changePowerStateToPriv() issued at the tail
3160 // end of SLEEP_STATE case should take care of that.
3161 if (getPowerState() == ON_STATE) {
3162 changePowerStateWithTagToPriv(ON_STATE, kCPSReasonWake);
3163 }
3164 break;
3165 }
3166 #endif /* !__i386__ && !__x86_64__ */
3167 }
3168 notifierThread = NULL;
3169 }
3170
3171 //******************************************************************************
3172 // requestPowerDomainState
3173 //
3174 // Extend implementation in IOService. Running on PM work loop thread.
3175 //******************************************************************************
3176
3177 IOReturn
requestPowerDomainState(IOPMPowerFlags childDesire,IOPowerConnection * childConnection,unsigned long specification)3178 IOPMrootDomain::requestPowerDomainState(
3179 IOPMPowerFlags childDesire,
3180 IOPowerConnection * childConnection,
3181 unsigned long specification )
3182 {
3183 // Idle and system sleep prevention flags affects driver desire.
3184 // Children desire are irrelevant so they are cleared.
3185
3186 return super::requestPowerDomainState(0, childConnection, specification);
3187 }
3188
3189
3190 static void
makeSleepPreventersListLog(const OSSharedPtr<OSSet> & preventers,char * buf,size_t buf_size)3191 makeSleepPreventersListLog(const OSSharedPtr<OSSet> &preventers, char *buf, size_t buf_size)
3192 {
3193 if (!preventers->getCount()) {
3194 return;
3195 }
3196
3197 char *buf_iter = buf + strlen(buf);
3198 char *buf_end = buf + buf_size;
3199
3200 OSSharedPtr<OSCollectionIterator> iterator = OSCollectionIterator::withCollection(preventers.get());
3201 OSObject *obj = NULL;
3202
3203 while ((obj = iterator->getNextObject())) {
3204 IOService *srv = OSDynamicCast(IOService, obj);
3205 if (buf_iter < buf_end) {
3206 buf_iter += snprintf(buf_iter, buf_end - buf_iter, " %s", srv->getName());
3207 } else {
3208 DLOG("Print buffer exhausted for sleep preventers list\n");
3209 break;
3210 }
3211 }
3212 }
3213
3214 //******************************************************************************
3215 // updatePreventIdleSleepList
3216 //
3217 // Called by IOService on PM work loop.
3218 // Returns true if PM policy recognized the driver's desire to prevent idle
3219 // sleep and updated the list of idle sleep preventers. Returns false otherwise
3220 //******************************************************************************
3221
3222 bool
updatePreventIdleSleepList(IOService * service,bool addNotRemove)3223 IOPMrootDomain::updatePreventIdleSleepList(
3224 IOService * service, bool addNotRemove)
3225 {
3226 unsigned int oldCount;
3227
3228 oldCount = idleSleepPreventersCount();
3229 return updatePreventIdleSleepListInternal(service, addNotRemove, oldCount);
3230 }
3231
3232 bool
updatePreventIdleSleepListInternal(IOService * service,bool addNotRemove,unsigned int oldCount)3233 IOPMrootDomain::updatePreventIdleSleepListInternal(
3234 IOService * service, bool addNotRemove, unsigned int oldCount)
3235 {
3236 unsigned int newCount;
3237
3238 ASSERT_GATED();
3239
3240 #if defined(XNU_TARGET_OS_OSX)
3241 // Only the display wrangler and no-idle-sleep kernel assertions
3242 // can prevent idle sleep. The kIOPMPreventIdleSleep capability flag
3243 // reported by drivers in their power state table is ignored.
3244 if (service && (service != wrangler) && (service != this)) {
3245 return false;
3246 }
3247 #endif
3248
3249 if (service) {
3250 if (addNotRemove) {
3251 preventIdleSleepList->setObject(service);
3252 DLOG("Added %s to idle sleep preventers list (Total %u)\n",
3253 service->getName(), preventIdleSleepList->getCount());
3254 } else if (preventIdleSleepList->member(service)) {
3255 preventIdleSleepList->removeObject(service);
3256 DLOG("Removed %s from idle sleep preventers list (Total %u)\n",
3257 service->getName(), preventIdleSleepList->getCount());
3258 }
3259
3260 if (preventIdleSleepList->getCount()) {
3261 char buf[256] = "Idle Sleep Preventers:";
3262 makeSleepPreventersListLog(preventIdleSleepList, buf, sizeof(buf));
3263 DLOG("%s\n", buf);
3264 }
3265 }
3266
3267 newCount = idleSleepPreventersCount();
3268
3269 if ((oldCount == 0) && (newCount != 0)) {
3270 // Driver added to empty prevent list.
3271 // Update the driver desire to prevent idle sleep.
3272 // Driver desire does not prevent demand sleep.
3273
3274 changePowerStateWithTagTo(getRUN_STATE(), kCPSReasonIdleSleepPrevent);
3275 } else if ((oldCount != 0) && (newCount == 0)) {
3276 // Last driver removed from prevent list.
3277 // Drop the driver clamp to allow idle sleep.
3278
3279 changePowerStateWithTagTo(SLEEP_STATE, kCPSReasonIdleSleepAllow);
3280 evaluatePolicy( kStimulusNoIdleSleepPreventers );
3281 }
3282 messageClient(kIOPMMessageIdleSleepPreventers, systemCapabilityNotifier.get(),
3283 &newCount, sizeof(newCount));
3284
3285 #if defined(XNU_TARGET_OS_OSX)
3286 if (addNotRemove && (service == wrangler) && !checkSystemCanSustainFullWake()) {
3287 DLOG("Cannot cancel idle sleep\n");
3288 return false; // do not idle-cancel
3289 }
3290 #endif
3291
3292 return true;
3293 }
3294
3295 //******************************************************************************
3296 // startSpinDump
3297 //******************************************************************************
3298
3299 void
startSpinDump(uint32_t spindumpKind)3300 IOPMrootDomain::startSpinDump(uint32_t spindumpKind)
3301 {
3302 messageClients(kIOPMMessageLaunchBootSpinDump, (void *)(uintptr_t)spindumpKind);
3303 }
3304
3305 //******************************************************************************
3306 // preventSystemSleepListUpdate
3307 //
3308 // Called by IOService on PM work loop.
3309 //******************************************************************************
3310
3311 void
updatePreventSystemSleepList(IOService * service,bool addNotRemove)3312 IOPMrootDomain::updatePreventSystemSleepList(
3313 IOService * service, bool addNotRemove )
3314 {
3315 unsigned int oldCount, newCount;
3316
3317 ASSERT_GATED();
3318 if (this == service) {
3319 return;
3320 }
3321
3322 oldCount = preventSystemSleepList->getCount();
3323 if (addNotRemove) {
3324 preventSystemSleepList->setObject(service);
3325 DLOG("Added %s to system sleep preventers list (Total %u)\n",
3326 service->getName(), preventSystemSleepList->getCount());
3327 if (!assertOnWakeSecs && gIOLastWakeAbsTime) {
3328 AbsoluteTime now;
3329 clock_usec_t microsecs;
3330 clock_get_uptime(&now);
3331 SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
3332 absolutetime_to_microtime(now, &assertOnWakeSecs, µsecs);
3333 if (assertOnWakeReport) {
3334 HISTREPORT_TALLYVALUE(assertOnWakeReport, (int64_t)assertOnWakeSecs);
3335 DLOG("Updated assertOnWake %lu\n", (unsigned long)assertOnWakeSecs);
3336 }
3337 }
3338 } else if (preventSystemSleepList->member(service)) {
3339 preventSystemSleepList->removeObject(service);
3340 DLOG("Removed %s from system sleep preventers list (Total %u)\n",
3341 service->getName(), preventSystemSleepList->getCount());
3342
3343 if ((oldCount != 0) && (preventSystemSleepList->getCount() == 0)) {
3344 // Lost all system sleep preventers.
3345 // Send stimulus if system sleep was blocked, and is in dark wake.
3346 evaluatePolicy( kStimulusDarkWakeEvaluate );
3347 }
3348 }
3349
3350 newCount = preventSystemSleepList->getCount();
3351 if (newCount) {
3352 char buf[256] = "System Sleep Preventers:";
3353 makeSleepPreventersListLog(preventSystemSleepList, buf, sizeof(buf));
3354 DLOG("%s\n", buf);
3355 }
3356
3357 messageClient(kIOPMMessageSystemSleepPreventers, systemCapabilityNotifier.get(),
3358 &newCount, sizeof(newCount));
3359 }
3360
3361 void
copySleepPreventersList(OSArray ** idleSleepList,OSArray ** systemSleepList)3362 IOPMrootDomain::copySleepPreventersList(OSArray **idleSleepList, OSArray **systemSleepList)
3363 {
3364 OSSharedPtr<OSCollectionIterator> iterator;
3365 OSObject *object = NULL;
3366 OSSharedPtr<OSArray> array;
3367
3368 if (!gIOPMWorkLoop->inGate()) {
3369 gIOPMWorkLoop->runAction(
3370 OSMemberFunctionCast(IOWorkLoop::Action, this,
3371 &IOPMrootDomain::IOPMrootDomain::copySleepPreventersList),
3372 this, (void *)idleSleepList, (void *)systemSleepList);
3373 return;
3374 }
3375
3376 if (idleSleepList && preventIdleSleepList && (preventIdleSleepList->getCount() != 0)) {
3377 iterator = OSCollectionIterator::withCollection(preventIdleSleepList.get());
3378 array = OSArray::withCapacity(5);
3379
3380 if (iterator && array) {
3381 while ((object = iterator->getNextObject())) {
3382 IOService *service = OSDynamicCast(IOService, object);
3383 if (service) {
3384 OSSharedPtr<const OSSymbol> name = service->copyName();
3385 if (name) {
3386 array->setObject(name.get());
3387 }
3388 }
3389 }
3390 }
3391 *idleSleepList = array.detach();
3392 }
3393
3394 if (systemSleepList && preventSystemSleepList && (preventSystemSleepList->getCount() != 0)) {
3395 iterator = OSCollectionIterator::withCollection(preventSystemSleepList.get());
3396 array = OSArray::withCapacity(5);
3397
3398 if (iterator && array) {
3399 while ((object = iterator->getNextObject())) {
3400 IOService *service = OSDynamicCast(IOService, object);
3401 if (service) {
3402 OSSharedPtr<const OSSymbol> name = service->copyName();
3403 if (name) {
3404 array->setObject(name.get());
3405 }
3406 }
3407 }
3408 }
3409 *systemSleepList = array.detach();
3410 }
3411 }
3412
3413 void
copySleepPreventersListWithID(OSArray ** idleSleepList,OSArray ** systemSleepList)3414 IOPMrootDomain::copySleepPreventersListWithID(OSArray **idleSleepList, OSArray **systemSleepList)
3415 {
3416 OSSharedPtr<OSCollectionIterator> iterator;
3417 OSObject *object = NULL;
3418 OSSharedPtr<OSArray> array;
3419
3420 if (!gIOPMWorkLoop->inGate()) {
3421 gIOPMWorkLoop->runAction(
3422 OSMemberFunctionCast(IOWorkLoop::Action, this,
3423 &IOPMrootDomain::IOPMrootDomain::copySleepPreventersListWithID),
3424 this, (void *)idleSleepList, (void *)systemSleepList);
3425 return;
3426 }
3427
3428 if (idleSleepList && preventIdleSleepList && (preventIdleSleepList->getCount() != 0)) {
3429 iterator = OSCollectionIterator::withCollection(preventIdleSleepList.get());
3430 array = OSArray::withCapacity(5);
3431
3432 if (iterator && array) {
3433 while ((object = iterator->getNextObject())) {
3434 IOService *service = OSDynamicCast(IOService, object);
3435 if (service) {
3436 OSSharedPtr<OSDictionary> dict = OSDictionary::withCapacity(2);
3437 OSSharedPtr<const OSSymbol> name = service->copyName();
3438 OSSharedPtr<OSNumber> id = OSNumber::withNumber(service->getRegistryEntryID(), 64);
3439 if (dict && name && id) {
3440 dict->setObject(kIOPMDriverAssertionRegistryEntryIDKey, id.get());
3441 dict->setObject(kIOPMDriverAssertionOwnerStringKey, name.get());
3442 array->setObject(dict.get());
3443 }
3444 }
3445 }
3446 }
3447 *idleSleepList = array.detach();
3448 }
3449
3450 if (systemSleepList && preventSystemSleepList && (preventSystemSleepList->getCount() != 0)) {
3451 iterator = OSCollectionIterator::withCollection(preventSystemSleepList.get());
3452 array = OSArray::withCapacity(5);
3453
3454 if (iterator && array) {
3455 while ((object = iterator->getNextObject())) {
3456 IOService *service = OSDynamicCast(IOService, object);
3457 if (service) {
3458 OSSharedPtr<OSDictionary> dict = OSDictionary::withCapacity(2);
3459 OSSharedPtr<const OSSymbol> name = service->copyName();
3460 OSSharedPtr<OSNumber> id = OSNumber::withNumber(service->getRegistryEntryID(), 64);
3461 if (dict && name && id) {
3462 dict->setObject(kIOPMDriverAssertionRegistryEntryIDKey, id.get());
3463 dict->setObject(kIOPMDriverAssertionOwnerStringKey, name.get());
3464 array->setObject(dict.get());
3465 }
3466 }
3467 }
3468 }
3469 *systemSleepList = array.detach();
3470 }
3471 }
3472
3473 //******************************************************************************
3474 // tellChangeDown
3475 //
3476 // Override the superclass implementation to send a different message type.
3477 //******************************************************************************
3478
3479 bool
tellChangeDown(unsigned long stateNum)3480 IOPMrootDomain::tellChangeDown( unsigned long stateNum )
3481 {
3482 DLOG("tellChangeDown %s->%s\n",
3483 getPowerStateString((uint32_t) getPowerState()), getPowerStateString((uint32_t) stateNum));
3484
3485 if (SLEEP_STATE == stateNum) {
3486 // Legacy apps were already told in the full->dark transition
3487 if (!ignoreTellChangeDown) {
3488 tracePoint( kIOPMTracePointSleepApplications );
3489 } else {
3490 tracePoint( kIOPMTracePointSleepPriorityClients );
3491 }
3492 }
3493
3494 if (!ignoreTellChangeDown) {
3495 userActivityAtSleep = userActivityCount;
3496 DLOG("tellChangeDown::userActivityAtSleep %d\n", userActivityAtSleep);
3497
3498 if (SLEEP_STATE == stateNum) {
3499 hibernateAborted = false;
3500
3501 // Direct callout into OSKext so it can disable kext unloads
3502 // during sleep/wake to prevent deadlocks.
3503 OSKextSystemSleepOrWake( kIOMessageSystemWillSleep );
3504
3505 IOService::updateConsoleUsers(NULL, kIOMessageSystemWillSleep);
3506
3507 // Two change downs are sent by IOServicePM. Ignore the 2nd.
3508 // But tellClientsWithResponse() must be called for both.
3509 ignoreTellChangeDown = true;
3510 }
3511 }
3512
3513 return super::tellClientsWithResponse( kIOMessageSystemWillSleep );
3514 }
3515
3516 //******************************************************************************
3517 // askChangeDown
3518 //
3519 // Override the superclass implementation to send a different message type.
3520 // This must be idle sleep since we don't ask during any other power change.
3521 //******************************************************************************
3522
3523 bool
askChangeDown(unsigned long stateNum)3524 IOPMrootDomain::askChangeDown( unsigned long stateNum )
3525 {
3526 DLOG("askChangeDown %s->%s\n",
3527 getPowerStateString((uint32_t) getPowerState()), getPowerStateString((uint32_t) stateNum));
3528
3529 // Don't log for dark wake entry
3530 if (kSystemTransitionSleep == _systemTransitionType) {
3531 tracePoint( kIOPMTracePointSleepApplications );
3532 }
3533
3534 return super::tellClientsWithResponse( kIOMessageCanSystemSleep );
3535 }
3536
3537 //******************************************************************************
3538 // askChangeDownDone
3539 //
3540 // An opportunity for root domain to cancel the power transition,
3541 // possibily due to an assertion created by powerd in response to
3542 // kIOMessageCanSystemSleep.
3543 //
3544 // Idle sleep:
3545 // full -> dark wake transition
3546 // 1. Notify apps and powerd with kIOMessageCanSystemSleep
3547 // 2. askChangeDownDone()
3548 // dark -> sleep transition
3549 // 1. Notify powerd with kIOMessageCanSystemSleep
3550 // 2. askChangeDownDone()
3551 //
3552 // Demand sleep:
3553 // full -> dark wake transition
3554 // 1. Notify powerd with kIOMessageCanSystemSleep
3555 // 2. askChangeDownDone()
3556 // dark -> sleep transition
3557 // 1. Notify powerd with kIOMessageCanSystemSleep
3558 // 2. askChangeDownDone()
3559 //******************************************************************************
3560
3561 void
askChangeDownDone(IOPMPowerChangeFlags * inOutChangeFlags,bool * cancel)3562 IOPMrootDomain::askChangeDownDone(
3563 IOPMPowerChangeFlags * inOutChangeFlags, bool * cancel )
3564 {
3565 DLOG("askChangeDownDone(0x%x, %u) type %x, cap %x->%x\n",
3566 *inOutChangeFlags, *cancel,
3567 _systemTransitionType,
3568 _currentCapability, _pendingCapability);
3569
3570 if ((false == *cancel) && (kSystemTransitionSleep == _systemTransitionType)) {
3571 // Dark->Sleep transition.
3572 // Check if there are any deny sleep assertions.
3573 // lastSleepReason already set by handleOurPowerChangeStart()
3574
3575 if (!checkSystemCanSleep(lastSleepReason)) {
3576 // Cancel dark wake to sleep transition.
3577 // Must re-scan assertions upon entering dark wake.
3578
3579 *cancel = true;
3580 DLOG("cancel dark->sleep\n");
3581 }
3582 if (_aotMode && (kPMCalendarTypeInvalid != _aotWakeTimeCalendar.selector)) {
3583 uint64_t now = mach_continuous_time();
3584 if (((now + _aotWakePreWindow) >= _aotWakeTimeContinuous)
3585 && (now < (_aotWakeTimeContinuous + _aotWakePostWindow))) {
3586 *cancel = true;
3587 IOLog("AOT wake window cancel: %qd, %qd\n", now, _aotWakeTimeContinuous);
3588 }
3589 }
3590 }
3591 }
3592
3593 //******************************************************************************
3594 // systemDidNotSleep
3595 //
3596 // Work common to both canceled or aborted sleep.
3597 //******************************************************************************
3598
3599 void
systemDidNotSleep(void)3600 IOPMrootDomain::systemDidNotSleep( void )
3601 {
3602 // reset console lock state
3603 thread_call_enter(updateConsoleUsersEntry);
3604
3605 if (idleSleepEnabled) {
3606 if (!wrangler) {
3607 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
3608 startIdleSleepTimer(kIdleSleepRetryInterval);
3609 #else
3610 startIdleSleepTimer(idleMilliSeconds);
3611 #endif
3612 } else if (!userIsActive) {
3613 // Manually start the idle sleep timer besides waiting for
3614 // the user to become inactive.
3615 startIdleSleepTimer(kIdleSleepRetryInterval);
3616 }
3617 }
3618
3619 preventTransitionToUserActive(false);
3620 IOService::setAdvisoryTickleEnable( true );
3621
3622 // After idle revert and cancel, send a did-change message to powerd
3623 // to balance the previous will-change message. Kernel clients do not
3624 // need this since sleep cannot be canceled once they are notified.
3625
3626 if (toldPowerdCapWillChange && systemCapabilityNotifier &&
3627 (_pendingCapability != _currentCapability) &&
3628 ((_systemMessageClientMask & kSystemMessageClientPowerd) != 0)) {
3629 // Differs from a real capability gain change where notifyRef != 0,
3630 // but it is zero here since no response is expected.
3631
3632 IOPMSystemCapabilityChangeParameters params;
3633
3634 bzero(¶ms, sizeof(params));
3635 params.fromCapabilities = _pendingCapability;
3636 params.toCapabilities = _currentCapability;
3637 params.changeFlags = kIOPMSystemCapabilityDidChange;
3638
3639 DLOG("MESG cap %x->%x did change\n",
3640 params.fromCapabilities, params.toCapabilities);
3641 messageClient(kIOMessageSystemCapabilityChange, systemCapabilityNotifier.get(),
3642 ¶ms, sizeof(params));
3643 }
3644 }
3645
3646 //******************************************************************************
3647 // tellNoChangeDown
3648 //
3649 // Notify registered applications and kernel clients that we are not dropping
3650 // power.
3651 //
3652 // We override the superclass implementation so we can send a different message
3653 // type to the client or application being notified.
3654 //
3655 // This must be a vetoed idle sleep, since no other power change can be vetoed.
3656 //******************************************************************************
3657
3658 void
tellNoChangeDown(unsigned long stateNum)3659 IOPMrootDomain::tellNoChangeDown( unsigned long stateNum )
3660 {
3661 DLOG("tellNoChangeDown %s->%s\n",
3662 getPowerStateString((uint32_t) getPowerState()), getPowerStateString((uint32_t) stateNum));
3663
3664 // Sleep canceled, clear the sleep trace point.
3665 tracePoint(kIOPMTracePointSystemUp);
3666
3667 systemDidNotSleep();
3668 return tellClients( kIOMessageSystemWillNotSleep );
3669 }
3670
3671 //******************************************************************************
3672 // tellChangeUp
3673 //
3674 // Notify registered applications and kernel clients that we are raising power.
3675 //
3676 // We override the superclass implementation so we can send a different message
3677 // type to the client or application being notified.
3678 //******************************************************************************
3679
3680 void
tellChangeUp(unsigned long stateNum)3681 IOPMrootDomain::tellChangeUp( unsigned long stateNum )
3682 {
3683 DLOG("tellChangeUp %s->%s\n",
3684 getPowerStateString((uint32_t) getPowerState()), getPowerStateString((uint32_t) stateNum));
3685
3686 ignoreTellChangeDown = false;
3687
3688 if (stateNum == ON_STATE) {
3689 // Direct callout into OSKext so it can disable kext unloads
3690 // during sleep/wake to prevent deadlocks.
3691 OSKextSystemSleepOrWake( kIOMessageSystemHasPoweredOn );
3692
3693 // Notify platform that sleep was cancelled or resumed.
3694 getPlatform()->callPlatformFunction(
3695 sleepMessagePEFunction.get(), false,
3696 (void *)(uintptr_t) kIOMessageSystemHasPoweredOn,
3697 NULL, NULL, NULL);
3698
3699 if (getPowerState() == ON_STATE) {
3700 // Sleep was cancelled by idle cancel or revert
3701 if (!CAP_CURRENT(kIOPMSystemCapabilityGraphics)) {
3702 // rdar://problem/50363791
3703 // If system is in dark wake and sleep is cancelled, do not
3704 // send SystemWillPowerOn/HasPoweredOn messages to kernel
3705 // priority clients. They haven't yet seen a SystemWillSleep
3706 // message before the cancellation. So make sure the kernel
3707 // client bit is cleared in _systemMessageClientMask before
3708 // invoking the tellClients() below. This bit may have been
3709 // set by handleOurPowerChangeStart() anticipating a successful
3710 // sleep and setting the filter mask ahead of time allows the
3711 // SystemWillSleep message to go through.
3712 _systemMessageClientMask &= ~kSystemMessageClientKernel;
3713 }
3714
3715 systemDidNotSleep();
3716 tellClients( kIOMessageSystemWillPowerOn );
3717 }
3718
3719 tracePoint( kIOPMTracePointWakeApplications );
3720 tellClients( kIOMessageSystemHasPoweredOn );
3721 } else if (stateNum == AOT_STATE) {
3722 if (getPowerState() == AOT_STATE) {
3723 // Sleep was cancelled by idle cancel or revert
3724 startIdleSleepTimer(idleMilliSeconds);
3725 }
3726 }
3727 }
3728
3729 #define CAP_WILL_CHANGE_TO_OFF(params, flag) \
3730 (((params)->changeFlags & kIOPMSystemCapabilityWillChange) && \
3731 ((params)->fromCapabilities & (flag)) && \
3732 (((params)->toCapabilities & (flag)) == 0))
3733
3734 #define CAP_DID_CHANGE_TO_ON(params, flag) \
3735 (((params)->changeFlags & kIOPMSystemCapabilityDidChange) && \
3736 ((params)->toCapabilities & (flag)) && \
3737 (((params)->fromCapabilities & (flag)) == 0))
3738
3739 #define CAP_DID_CHANGE_TO_OFF(params, flag) \
3740 (((params)->changeFlags & kIOPMSystemCapabilityDidChange) && \
3741 ((params)->fromCapabilities & (flag)) && \
3742 (((params)->toCapabilities & (flag)) == 0))
3743
3744 #define CAP_WILL_CHANGE_TO_ON(params, flag) \
3745 (((params)->changeFlags & kIOPMSystemCapabilityWillChange) && \
3746 ((params)->toCapabilities & (flag)) && \
3747 (((params)->fromCapabilities & (flag)) == 0))
3748
3749 //******************************************************************************
3750 // sysPowerDownHandler
3751 //
3752 // Perform a vfs sync before system sleep.
3753 //******************************************************************************
3754
3755 IOReturn
sysPowerDownHandler(void * target,void * refCon,UInt32 messageType,IOService * service,void * messageArgs,vm_size_t argSize)3756 IOPMrootDomain::sysPowerDownHandler(
3757 void * target, void * refCon,
3758 UInt32 messageType, IOService * service,
3759 void * messageArgs, vm_size_t argSize )
3760 {
3761 static UInt32 lastSystemMessageType = 0;
3762 IOReturn ret = 0;
3763
3764 DLOG("sysPowerDownHandler message %s\n", getIOMessageString(messageType));
3765
3766 // rdar://problem/50363791
3767 // Sanity check to make sure the SystemWill/Has message types are
3768 // received in the expected order for all kernel priority clients.
3769 if (messageType == kIOMessageSystemWillSleep ||
3770 messageType == kIOMessageSystemWillPowerOn ||
3771 messageType == kIOMessageSystemHasPoweredOn) {
3772 switch (messageType) {
3773 case kIOMessageSystemWillPowerOn:
3774 assert(lastSystemMessageType == kIOMessageSystemWillSleep);
3775 break;
3776 case kIOMessageSystemHasPoweredOn:
3777 assert(lastSystemMessageType == kIOMessageSystemWillPowerOn);
3778 break;
3779 }
3780
3781 lastSystemMessageType = messageType;
3782 }
3783
3784 if (!gRootDomain) {
3785 return kIOReturnUnsupported;
3786 }
3787
3788 if (messageType == kIOMessageSystemCapabilityChange) {
3789 IOPMSystemCapabilityChangeParameters * params =
3790 (IOPMSystemCapabilityChangeParameters *) messageArgs;
3791
3792 // Interested applications have been notified of an impending power
3793 // change and have acked (when applicable).
3794 // This is our chance to save whatever state we can before powering
3795 // down.
3796 // We call sync_internal defined in xnu/bsd/vfs/vfs_syscalls.c,
3797 // via callout
3798
3799 DLOG("sysPowerDownHandler cap %x -> %x (flags %x)\n",
3800 params->fromCapabilities, params->toCapabilities,
3801 params->changeFlags);
3802
3803 if (CAP_WILL_CHANGE_TO_OFF(params, kIOPMSystemCapabilityCPU)) {
3804 // We will ack within 20 seconds
3805 params->maxWaitForReply = 20 * 1000 * 1000;
3806
3807 #if HIBERNATION
3808 gRootDomain->evaluateSystemSleepPolicyEarly();
3809
3810 // add in time we could spend freeing pages
3811 if (gRootDomain->hibernateMode && !gRootDomain->hibernateDisabled) {
3812 params->maxWaitForReply = kCapabilityClientMaxWait;
3813 }
3814 DLOG("sysPowerDownHandler max wait %d s\n",
3815 (int) (params->maxWaitForReply / 1000 / 1000));
3816 #endif
3817
3818 // Notify platform that sleep has begun, after the early
3819 // sleep policy evaluation.
3820 getPlatform()->callPlatformFunction(
3821 sleepMessagePEFunction.get(), false,
3822 (void *)(uintptr_t) kIOMessageSystemWillSleep,
3823 NULL, NULL, NULL);
3824
3825 if (!OSCompareAndSwap( 0, 1, &gSleepOrShutdownPending )) {
3826 // Purposely delay the ack and hope that shutdown occurs quickly.
3827 // Another option is not to schedule the thread and wait for
3828 // ack timeout...
3829 AbsoluteTime deadline;
3830 clock_interval_to_deadline( 30, kSecondScale, &deadline );
3831 thread_call_enter1_delayed(
3832 gRootDomain->diskSyncCalloutEntry,
3833 (thread_call_param_t)(uintptr_t) params->notifyRef,
3834 deadline );
3835 } else {
3836 thread_call_enter1(
3837 gRootDomain->diskSyncCalloutEntry,
3838 (thread_call_param_t)(uintptr_t) params->notifyRef);
3839 }
3840 }
3841 #if HIBERNATION
3842 else if (CAP_DID_CHANGE_TO_ON(params, kIOPMSystemCapabilityCPU)) {
3843 // We will ack within 110 seconds
3844 params->maxWaitForReply = 110 * 1000 * 1000;
3845
3846 thread_call_enter1(
3847 gRootDomain->diskSyncCalloutEntry,
3848 (thread_call_param_t)(uintptr_t) params->notifyRef);
3849 }
3850 #endif
3851 ret = kIOReturnSuccess;
3852 }
3853
3854 return ret;
3855 }
3856
3857 //******************************************************************************
3858 // handleQueueSleepWakeUUID
3859 //
3860 // Called from IOPMrootDomain when we're initiating a sleep,
3861 // or indirectly from PM configd when PM decides to clear the UUID.
3862 // PM clears the UUID several minutes after successful wake from sleep,
3863 // so that we might associate App spindumps with the immediately previous
3864 // sleep/wake.
3865 //
3866 // @param obj has a retain on it. We're responsible for releasing that retain.
3867 //******************************************************************************
3868
3869 void
handleQueueSleepWakeUUID(OSObject * obj)3870 IOPMrootDomain::handleQueueSleepWakeUUID(OSObject *obj)
3871 {
3872 OSSharedPtr<OSString> str;
3873
3874 if (kOSBooleanFalse == obj) {
3875 handlePublishSleepWakeUUID(false);
3876 } else {
3877 str.reset(OSDynamicCast(OSString, obj), OSNoRetain);
3878 if (str) {
3879 // This branch caches the UUID for an upcoming sleep/wake
3880 queuedSleepWakeUUIDString = str;
3881 DLOG("SleepWake UUID queued: %s\n", queuedSleepWakeUUIDString->getCStringNoCopy());
3882 }
3883 }
3884 }
3885 //******************************************************************************
3886 // handlePublishSleepWakeUUID
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
3895 void
handlePublishSleepWakeUUID(bool shouldPublish)3896 IOPMrootDomain::handlePublishSleepWakeUUID( bool shouldPublish )
3897 {
3898 ASSERT_GATED();
3899
3900 /*
3901 * Clear the current UUID
3902 */
3903 if (gSleepWakeUUIDIsSet) {
3904 DLOG("SleepWake UUID cleared\n");
3905
3906 gSleepWakeUUIDIsSet = false;
3907
3908 removeProperty(kIOPMSleepWakeUUIDKey);
3909 messageClients(kIOPMMessageSleepWakeUUIDChange, kIOPMMessageSleepWakeUUIDCleared);
3910 }
3911
3912 /*
3913 * Optionally, publish a new UUID
3914 */
3915 if (queuedSleepWakeUUIDString && shouldPublish) {
3916 OSSharedPtr<OSString> publishThisUUID;
3917
3918 publishThisUUID = queuedSleepWakeUUIDString;
3919
3920 if (publishThisUUID) {
3921 setProperty(kIOPMSleepWakeUUIDKey, publishThisUUID.get());
3922 }
3923
3924 gSleepWakeUUIDIsSet = true;
3925 messageClients(kIOPMMessageSleepWakeUUIDChange, kIOPMMessageSleepWakeUUIDSet);
3926
3927 queuedSleepWakeUUIDString.reset();
3928 }
3929 }
3930
3931 //******************************************************************************
3932 // IOPMGetSleepWakeUUIDKey
3933 //
3934 // Return the truth value of gSleepWakeUUIDIsSet and optionally copy the key.
3935 // To get the full key -- a C string -- the buffer must large enough for
3936 // the end-of-string character.
3937 // The key is expected to be an UUID string
3938 //******************************************************************************
3939
3940 extern "C" bool
IOPMCopySleepWakeUUIDKey(char * buffer,size_t buf_len)3941 IOPMCopySleepWakeUUIDKey(char *buffer, size_t buf_len)
3942 {
3943 if (!gSleepWakeUUIDIsSet) {
3944 return false;
3945 }
3946
3947 if (buffer != NULL) {
3948 OSSharedPtr<OSString> string =
3949 OSDynamicPtrCast<OSString>(gRootDomain->copyProperty(kIOPMSleepWakeUUIDKey));
3950
3951 if (!string) {
3952 *buffer = '\0';
3953 } else {
3954 strlcpy(buffer, string->getCStringNoCopy(), buf_len);
3955 }
3956 }
3957
3958 return true;
3959 }
3960
3961 //******************************************************************************
3962 // lowLatencyAudioNotify
3963 //
3964 // Used to send an update about low latency audio activity to interested
3965 // clients. To keep the overhead minimal the OSDictionary used here
3966 // is initialized at boot.
3967 //******************************************************************************
3968
3969 void
lowLatencyAudioNotify(uint64_t time,boolean_t state)3970 IOPMrootDomain::lowLatencyAudioNotify(uint64_t time, boolean_t state)
3971 {
3972 if (lowLatencyAudioNotifierDict && lowLatencyAudioNotifyStateSym && lowLatencyAudioNotifyTimestampSym &&
3973 lowLatencyAudioNotifyStateVal && lowLatencyAudioNotifyTimestampVal) {
3974 lowLatencyAudioNotifyTimestampVal->setValue(time);
3975 lowLatencyAudioNotifyStateVal->setValue(state);
3976 setPMSetting(gIOPMSettingLowLatencyAudioModeKey.get(), lowLatencyAudioNotifierDict.get());
3977 } else {
3978 DLOG("LowLatencyAudioNotify error\n");
3979 }
3980 return;
3981 }
3982
3983 //******************************************************************************
3984 // IOPMrootDomainRTNotifier
3985 //
3986 // Used by performance controller to update the timestamp and state associated
3987 // with low latency audio activity in the system.
3988 //******************************************************************************
3989
3990 extern "C" void
IOPMrootDomainRTNotifier(uint64_t time,boolean_t state)3991 IOPMrootDomainRTNotifier(uint64_t time, boolean_t state)
3992 {
3993 gRootDomain->lowLatencyAudioNotify(time, state);
3994 return;
3995 }
3996
3997 //******************************************************************************
3998 // initializeBootSessionUUID
3999 //
4000 // Initialize the boot session uuid at boot up and sets it into registry.
4001 //******************************************************************************
4002
4003 void
initializeBootSessionUUID(void)4004 IOPMrootDomain::initializeBootSessionUUID(void)
4005 {
4006 uuid_t new_uuid;
4007 uuid_string_t new_uuid_string;
4008
4009 uuid_generate(new_uuid);
4010 uuid_unparse_upper(new_uuid, new_uuid_string);
4011 memcpy(bootsessionuuid_string, new_uuid_string, sizeof(uuid_string_t));
4012
4013 setProperty(kIOPMBootSessionUUIDKey, new_uuid_string);
4014 }
4015
4016 //******************************************************************************
4017 // Root domain uses the private and tagged changePowerState methods for
4018 // tracking and logging purposes.
4019 //******************************************************************************
4020
4021 #define REQUEST_TAG_TO_REASON(x) ((uint16_t)x)
4022
4023 static uint32_t
nextRequestTag(IOPMRequestTag tag)4024 nextRequestTag( IOPMRequestTag tag )
4025 {
4026 static SInt16 msb16 = 1;
4027 uint16_t id = OSAddAtomic16(1, &msb16);
4028 return ((uint32_t)id << 16) | REQUEST_TAG_TO_REASON(tag);
4029 }
4030
4031 // TODO: remove this shim function and exported symbol
4032 IOReturn
changePowerStateTo(unsigned long ordinal)4033 IOPMrootDomain::changePowerStateTo( unsigned long ordinal )
4034 {
4035 return changePowerStateWithTagTo(ordinal, kCPSReasonNone);
4036 }
4037
4038 // TODO: remove this shim function and exported symbol
4039 IOReturn
changePowerStateToPriv(unsigned long ordinal)4040 IOPMrootDomain::changePowerStateToPriv( unsigned long ordinal )
4041 {
4042 return changePowerStateWithTagToPriv(ordinal, kCPSReasonNone);
4043 }
4044
4045 IOReturn
changePowerStateWithOverrideTo(IOPMPowerStateIndex ordinal,IOPMRequestTag reason)4046 IOPMrootDomain::changePowerStateWithOverrideTo(
4047 IOPMPowerStateIndex ordinal, IOPMRequestTag reason )
4048 {
4049 uint32_t tag = nextRequestTag(reason);
4050 DLOG("%s(%s, %x)\n", __FUNCTION__, getPowerStateString((uint32_t) ordinal), tag);
4051
4052 if ((ordinal != ON_STATE) && (ordinal != AOT_STATE) && (ordinal != SLEEP_STATE)) {
4053 return kIOReturnUnsupported;
4054 }
4055
4056 return super::changePowerStateWithOverrideTo(ordinal, tag);
4057 }
4058
4059 IOReturn
changePowerStateWithTagTo(IOPMPowerStateIndex ordinal,IOPMRequestTag reason)4060 IOPMrootDomain::changePowerStateWithTagTo(
4061 IOPMPowerStateIndex ordinal, IOPMRequestTag reason )
4062 {
4063 uint32_t tag = nextRequestTag(reason);
4064 DLOG("%s(%s, %x)\n", __FUNCTION__, getPowerStateString((uint32_t) ordinal), tag);
4065
4066 if ((ordinal != ON_STATE) && (ordinal != AOT_STATE) && (ordinal != SLEEP_STATE)) {
4067 return kIOReturnUnsupported;
4068 }
4069
4070 return super::changePowerStateWithTagTo(ordinal, tag);
4071 }
4072
4073 IOReturn
changePowerStateWithTagToPriv(IOPMPowerStateIndex ordinal,IOPMRequestTag reason)4074 IOPMrootDomain::changePowerStateWithTagToPriv(
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::changePowerStateWithTagToPriv(ordinal, tag);
4085 }
4086
4087 //******************************************************************************
4088 // activity detect
4089 //
4090 //******************************************************************************
4091
4092 bool
activitySinceSleep(void)4093 IOPMrootDomain::activitySinceSleep(void)
4094 {
4095 return userActivityCount != userActivityAtSleep;
4096 }
4097
4098 bool
abortHibernation(void)4099 IOPMrootDomain::abortHibernation(void)
4100 {
4101 #if __arm64__
4102 // don't allow hibernation to be aborted on ARM due to user activity
4103 // since once ApplePMGR decides we're hibernating, we can't turn back
4104 // see: <rdar://problem/63848862> Tonga ApplePMGR diff quiesce path support
4105 return false;
4106 #else
4107 bool ret = activitySinceSleep();
4108
4109 if (ret && !hibernateAborted && checkSystemCanSustainFullWake()) {
4110 DLOG("activitySinceSleep ABORT [%d, %d]\n", userActivityCount, userActivityAtSleep);
4111 hibernateAborted = true;
4112 }
4113 return ret;
4114 #endif
4115 }
4116
4117 extern "C" int
hibernate_should_abort(void)4118 hibernate_should_abort(void)
4119 {
4120 if (gRootDomain) {
4121 return gRootDomain->abortHibernation();
4122 } else {
4123 return 0;
4124 }
4125 }
4126
4127 //******************************************************************************
4128 // willNotifyPowerChildren
4129 //
4130 // Called after all interested drivers have all acknowledged the power change,
4131 // but before any power children is informed. Dispatched though a thread call,
4132 // so it is safe to perform work that might block on a sleeping disk. PM state
4133 // machine (not thread) will block w/o timeout until this function returns.
4134 //******************************************************************************
4135
4136 void
willNotifyPowerChildren(IOPMPowerStateIndex newPowerState)4137 IOPMrootDomain::willNotifyPowerChildren( IOPMPowerStateIndex newPowerState )
4138 {
4139 OSSharedPtr<OSDictionary> dict;
4140 OSSharedPtr<OSNumber> secs;
4141
4142 if (SLEEP_STATE == newPowerState) {
4143 notifierThread = current_thread();
4144 if (updateTasksSuspend(kTasksSuspendSuspended, kTasksSuspendNoChange)) {
4145 AbsoluteTime deadline;
4146
4147 clock_interval_to_deadline(10, kSecondScale, &deadline);
4148 #if defined(XNU_TARGET_OS_OSX)
4149 vm_pageout_wait(AbsoluteTime_to_scalar(&deadline));
4150 #endif /* defined(XNU_TARGET_OS_OSX) */
4151 }
4152
4153 _aotReadyToFullWake = false;
4154 #if 0
4155 if (_aotLingerTime) {
4156 uint64_t deadline;
4157 IOLog("aot linger no return\n");
4158 clock_absolutetime_interval_to_deadline(_aotLingerTime, &deadline);
4159 clock_delay_until(deadline);
4160 }
4161 #endif
4162 if (!_aotMode) {
4163 _aotTestTime = 0;
4164 _aotWakeTimeCalendar.selector = kPMCalendarTypeInvalid;
4165 _aotLastWakeTime = 0;
4166 if (_aotMetrics) {
4167 bzero(_aotMetrics, sizeof(IOPMAOTMetrics));
4168 }
4169 } else if (!_aotNow && !_debugWakeSeconds) {
4170 _aotNow = true;
4171 _aotPendingFlags = 0;
4172 _aotTasksSuspended = true;
4173 _aotLastWakeTime = 0;
4174 bzero(_aotMetrics, sizeof(IOPMAOTMetrics));
4175 if (kIOPMAOTModeCycle & _aotMode) {
4176 clock_interval_to_absolutetime_interval(60, kSecondScale, &_aotTestInterval);
4177 _aotTestTime = mach_continuous_time() + _aotTestInterval;
4178 setWakeTime(_aotTestTime);
4179 }
4180 uint32_t lingerSecs;
4181 if (!PE_parse_boot_argn("aotlinger", &lingerSecs, sizeof(lingerSecs))) {
4182 lingerSecs = 0;
4183 }
4184 clock_interval_to_absolutetime_interval(lingerSecs, kSecondScale, &_aotLingerTime);
4185 clock_interval_to_absolutetime_interval(2000, kMillisecondScale, &_aotWakePreWindow);
4186 clock_interval_to_absolutetime_interval(1100, kMillisecondScale, &_aotWakePostWindow);
4187 }
4188
4189 #if HIBERNATION
4190 IOHibernateSystemSleep();
4191 IOHibernateIOKitSleep();
4192 #endif
4193 if (gRootDomain->activitySinceSleep()) {
4194 dict = OSDictionary::withCapacity(1);
4195 secs = OSNumber::withNumber(1, 32);
4196
4197 if (dict && secs) {
4198 dict->setObject(gIOPMSettingDebugWakeRelativeKey.get(), secs.get());
4199 gRootDomain->setProperties(dict.get());
4200 MSG("Reverting sleep with relative wake\n");
4201 }
4202 }
4203
4204 notifierThread = NULL;
4205 }
4206 }
4207
4208 //******************************************************************************
4209 // willTellSystemCapabilityDidChange
4210 //
4211 // IOServicePM calls this from OurChangeTellCapabilityDidChange() when root
4212 // domain is raising its power state, immediately after notifying interested
4213 // drivers and power children.
4214 //******************************************************************************
4215
4216 void
willTellSystemCapabilityDidChange(void)4217 IOPMrootDomain::willTellSystemCapabilityDidChange( void )
4218 {
4219 if ((_systemTransitionType == kSystemTransitionWake) &&
4220 !CAP_GAIN(kIOPMSystemCapabilityGraphics)) {
4221 // After powering up drivers, dark->full promotion on the current wake
4222 // transition is no longer possible. That is because the next machine
4223 // state will issue the system capability change messages.
4224 // The darkWakePowerClamped flag may already be set if the system has
4225 // at least one driver that was power clamped due to dark wake.
4226 // This function sets the darkWakePowerClamped flag in case there
4227 // is no power-clamped driver in the system.
4228 //
4229 // Last opportunity to exit dark wake using:
4230 // requestFullWake( kFullWakeReasonLocalUser );
4231
4232 if (!darkWakePowerClamped) {
4233 if (darkWakeLogClamp) {
4234 AbsoluteTime now;
4235 uint64_t nsec;
4236
4237 clock_get_uptime(&now);
4238 SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
4239 absolutetime_to_nanoseconds(now, &nsec);
4240 DLOG("dark wake promotion disabled at %u ms\n",
4241 ((int)((nsec) / NSEC_PER_MSEC)));
4242 }
4243 darkWakePowerClamped = true;
4244 }
4245 }
4246 }
4247
4248 //******************************************************************************
4249 // sleepOnClamshellClosed
4250 //
4251 // contains the logic to determine if the system should sleep when the clamshell
4252 // is closed.
4253 //******************************************************************************
4254
4255 bool
shouldSleepOnClamshellClosed(void)4256 IOPMrootDomain::shouldSleepOnClamshellClosed( void )
4257 {
4258 if (!clamshellExists) {
4259 return false;
4260 }
4261
4262 DLOG("clamshell closed %d, disabled %d/%x, desktopMode %d, ac %d\n",
4263 clamshellClosed, clamshellDisabled, clamshellSleepDisableMask, desktopMode, acAdaptorConnected);
4264
4265 return !clamshellDisabled && !(desktopMode && acAdaptorConnected) && !clamshellSleepDisableMask;
4266 }
4267
4268 bool
shouldSleepOnRTCAlarmWake(void)4269 IOPMrootDomain::shouldSleepOnRTCAlarmWake( void )
4270 {
4271 // Called once every RTC/Alarm wake. Device should go to sleep if on clamshell
4272 // closed && battery
4273 if (!clamshellExists) {
4274 return false;
4275 }
4276
4277 DLOG("shouldSleepOnRTCAlarmWake: clamshell closed %d, disabled %d/%x, desktopMode %d, ac %d\n",
4278 clamshellClosed, clamshellDisabled, clamshellSleepDisableMask, desktopMode, acAdaptorConnected);
4279
4280 return !acAdaptorConnected && !clamshellSleepDisableMask;
4281 }
4282
4283 void
sendClientClamshellNotification(void)4284 IOPMrootDomain::sendClientClamshellNotification( void )
4285 {
4286 /* Only broadcast clamshell alert if clamshell exists. */
4287 if (!clamshellExists) {
4288 return;
4289 }
4290
4291 setProperty(kAppleClamshellStateKey,
4292 clamshellClosed ? kOSBooleanTrue : kOSBooleanFalse);
4293
4294 setProperty(kAppleClamshellCausesSleepKey,
4295 shouldSleepOnClamshellClosed() ? kOSBooleanTrue : kOSBooleanFalse);
4296
4297 /* Argument to message is a bitfiel of
4298 * ( kClamshellStateBit | kClamshellSleepBit )
4299 */
4300 messageClients(kIOPMMessageClamshellStateChange,
4301 (void *)(uintptr_t) ((clamshellClosed ? kClamshellStateBit : 0)
4302 | (shouldSleepOnClamshellClosed() ? kClamshellSleepBit : 0)));
4303 }
4304
4305 //******************************************************************************
4306 // getSleepSupported
4307 //
4308 // Deprecated
4309 //******************************************************************************
4310
4311 IOOptionBits
getSleepSupported(void)4312 IOPMrootDomain::getSleepSupported( void )
4313 {
4314 return platformSleepSupport;
4315 }
4316
4317 //******************************************************************************
4318 // setSleepSupported
4319 //
4320 // Deprecated
4321 //******************************************************************************
4322
4323 void
setSleepSupported(IOOptionBits flags)4324 IOPMrootDomain::setSleepSupported( IOOptionBits flags )
4325 {
4326 DLOG("setSleepSupported(%x)\n", (uint32_t) flags);
4327 OSBitOrAtomic(flags, &platformSleepSupport);
4328 }
4329
4330 //******************************************************************************
4331 // setClamShellSleepDisable
4332 //
4333 //******************************************************************************
4334
4335 void
setClamShellSleepDisable(bool disable,uint32_t bitmask)4336 IOPMrootDomain::setClamShellSleepDisable( bool disable, uint32_t bitmask )
4337 {
4338 uint32_t oldMask;
4339
4340 // User client calls this in non-gated context
4341 if (gIOPMWorkLoop->inGate() == false) {
4342 gIOPMWorkLoop->runAction(
4343 OSMemberFunctionCast(IOWorkLoop::Action, this,
4344 &IOPMrootDomain::setClamShellSleepDisable),
4345 (OSObject *) this,
4346 (void *) disable, (void *)(uintptr_t) bitmask);
4347 return;
4348 }
4349
4350 oldMask = clamshellSleepDisableMask;
4351 if (disable) {
4352 clamshellSleepDisableMask |= bitmask;
4353 } else {
4354 clamshellSleepDisableMask &= ~bitmask;
4355 }
4356 DLOG("setClamShellSleepDisable(%x->%x)\n", oldMask, clamshellSleepDisableMask);
4357
4358 if (clamshellExists && clamshellClosed &&
4359 (clamshellSleepDisableMask != oldMask) &&
4360 (clamshellSleepDisableMask == 0)) {
4361 handlePowerNotification(kLocalEvalClamshellCommand);
4362 }
4363 }
4364
4365 //******************************************************************************
4366 // wakeFromDoze
4367 //
4368 // Deprecated.
4369 //******************************************************************************
4370
4371 void
wakeFromDoze(void)4372 IOPMrootDomain::wakeFromDoze( void )
4373 {
4374 // Preserve symbol for familes (IOUSBFamily and IOGraphics)
4375 }
4376
4377 //******************************************************************************
4378 // recordRTCAlarm
4379 //
4380 // Record the earliest scheduled RTC alarm to determine whether a RTC wake
4381 // should be a dark wake or a full wake. Both Maintenance and SleepService
4382 // alarms are dark wake, while AutoWake (WakeByCalendarDate) and DebugWake
4383 // (WakeRelativeToSleep) should trigger a full wake. Scheduled power-on
4384 // PMSettings are ignored.
4385 //
4386 // Caller serialized using settingsCtrlLock.
4387 //******************************************************************************
4388
4389 void
recordRTCAlarm(const OSSymbol * type,OSObject * object)4390 IOPMrootDomain::recordRTCAlarm(
4391 const OSSymbol *type,
4392 OSObject *object )
4393 {
4394 uint32_t previousAlarmMask = _scheduledAlarmMask;
4395
4396 if (type == gIOPMSettingDebugWakeRelativeKey) {
4397 OSNumber * n = OSDynamicCast(OSNumber, object);
4398 if (n) {
4399 // Debug wake has highest scheduling priority so it overrides any
4400 // pre-existing alarm.
4401 uint32_t debugSecs = n->unsigned32BitValue();
4402 _nextScheduledAlarmType.reset(type, OSRetain);
4403 _nextScheduledAlarmUTC = debugSecs;
4404
4405 _debugWakeSeconds = debugSecs;
4406 OSBitOrAtomic(kIOPMAlarmBitDebugWake, &_scheduledAlarmMask);
4407 DLOG("next alarm (%s) in %u secs\n",
4408 type->getCStringNoCopy(), debugSecs);
4409 }
4410 } else if ((type == gIOPMSettingAutoWakeCalendarKey.get()) ||
4411 (type == gIOPMSettingMaintenanceWakeCalendarKey.get()) ||
4412 (type == gIOPMSettingSleepServiceWakeCalendarKey.get())) {
4413 OSData * data = OSDynamicCast(OSData, object);
4414 if (data && (data->getLength() == sizeof(IOPMCalendarStruct))) {
4415 const IOPMCalendarStruct * cs;
4416 bool replaceNextAlarm = false;
4417 clock_sec_t secs;
4418
4419 cs = (const IOPMCalendarStruct *) data->getBytesNoCopy();
4420 secs = IOPMConvertCalendarToSeconds(cs);
4421 DLOG("%s " YMDTF "\n", type->getCStringNoCopy(), YMDT(cs));
4422
4423 // Update the next scheduled alarm type
4424 if ((_nextScheduledAlarmType == NULL) ||
4425 ((_nextScheduledAlarmType != gIOPMSettingDebugWakeRelativeKey) &&
4426 (secs < _nextScheduledAlarmUTC))) {
4427 replaceNextAlarm = true;
4428 }
4429
4430 if (type == gIOPMSettingAutoWakeCalendarKey.get()) {
4431 if (cs->year) {
4432 _calendarWakeAlarmUTC = IOPMConvertCalendarToSeconds(cs);
4433 OSBitOrAtomic(kIOPMAlarmBitCalendarWake, &_scheduledAlarmMask);
4434 } else {
4435 // TODO: can this else-block be removed?
4436 _calendarWakeAlarmUTC = 0;
4437 OSBitAndAtomic(~kIOPMAlarmBitCalendarWake, &_scheduledAlarmMask);
4438 }
4439 }
4440 if (type == gIOPMSettingMaintenanceWakeCalendarKey.get()) {
4441 OSBitOrAtomic(kIOPMAlarmBitMaintenanceWake, &_scheduledAlarmMask);
4442 }
4443 if (type == gIOPMSettingSleepServiceWakeCalendarKey.get()) {
4444 OSBitOrAtomic(kIOPMAlarmBitSleepServiceWake, &_scheduledAlarmMask);
4445 }
4446
4447 if (replaceNextAlarm) {
4448 _nextScheduledAlarmType.reset(type, OSRetain);
4449 _nextScheduledAlarmUTC = secs;
4450 DLOG("next alarm (%s) " YMDTF "\n", type->getCStringNoCopy(), YMDT(cs));
4451 }
4452 }
4453 }
4454
4455 if (_scheduledAlarmMask != previousAlarmMask) {
4456 DLOG("scheduled alarm mask 0x%x\n", (uint32_t) _scheduledAlarmMask);
4457 }
4458 }
4459
4460 // MARK: -
4461 // MARK: Features
4462
4463 //******************************************************************************
4464 // publishFeature
4465 //
4466 // Adds a new feature to the supported features dictionary
4467 //******************************************************************************
4468
4469 void
publishFeature(const char * feature)4470 IOPMrootDomain::publishFeature( const char * feature )
4471 {
4472 publishFeature(feature, kRD_AllPowerSources, NULL);
4473 }
4474
4475 //******************************************************************************
4476 // publishFeature (with supported power source specified)
4477 //
4478 // Adds a new feature to the supported features dictionary
4479 //******************************************************************************
4480
4481 void
publishFeature(const char * feature,uint32_t supportedWhere,uint32_t * uniqueFeatureID)4482 IOPMrootDomain::publishFeature(
4483 const char *feature,
4484 uint32_t supportedWhere,
4485 uint32_t *uniqueFeatureID)
4486 {
4487 static uint16_t next_feature_id = 500;
4488
4489 OSSharedPtr<OSNumber> new_feature_data;
4490 OSNumber *existing_feature = NULL;
4491 OSArray *existing_feature_arr_raw = NULL;
4492 OSSharedPtr<OSArray> existing_feature_arr;
4493 OSObject *osObj = NULL;
4494 uint32_t feature_value = 0;
4495
4496 supportedWhere &= kRD_AllPowerSources; // mask off any craziness!
4497
4498 if (!supportedWhere) {
4499 // Feature isn't supported anywhere!
4500 return;
4501 }
4502
4503 if (next_feature_id > 5000) {
4504 // Far, far too many features!
4505 return;
4506 }
4507
4508 if (featuresDictLock) {
4509 IOLockLock(featuresDictLock);
4510 }
4511
4512 OSSharedPtr<OSObject> origFeaturesProp = copyProperty(kRootDomainSupportedFeatures);
4513 OSDictionary *origFeatures = OSDynamicCast(OSDictionary, origFeaturesProp.get());
4514 OSSharedPtr<OSDictionary> features;
4515
4516 // Create new features dict if necessary
4517 if (origFeatures) {
4518 features = OSDictionary::withDictionary(origFeatures);
4519 } else {
4520 features = OSDictionary::withCapacity(1);
4521 }
4522
4523 // Create OSNumber to track new feature
4524
4525 next_feature_id += 1;
4526 if (uniqueFeatureID) {
4527 // We don't really mind if the calling kext didn't give us a place
4528 // to stash their unique id. Many kexts don't plan to unload, and thus
4529 // have no need to remove themselves later.
4530 *uniqueFeatureID = next_feature_id;
4531 }
4532
4533 feature_value = (uint32_t)next_feature_id;
4534 feature_value <<= 16;
4535 feature_value += supportedWhere;
4536
4537 new_feature_data = OSNumber::withNumber(
4538 (unsigned long long)feature_value, 32);
4539
4540 // Does features object already exist?
4541 if ((osObj = features->getObject(feature))) {
4542 if ((existing_feature = OSDynamicCast(OSNumber, osObj))) {
4543 // We need to create an OSArray to hold the now 2 elements.
4544 existing_feature_arr = OSArray::withObjects(
4545 (const OSObject **)&existing_feature, 1, 2);
4546 } else if ((existing_feature_arr_raw = OSDynamicCast(OSArray, osObj))) {
4547 // Add object to existing array
4548 existing_feature_arr = OSArray::withArray(
4549 existing_feature_arr_raw,
4550 existing_feature_arr_raw->getCount() + 1);
4551 }
4552
4553 if (existing_feature_arr) {
4554 existing_feature_arr->setObject(new_feature_data.get());
4555 features->setObject(feature, existing_feature_arr.get());
4556 }
4557 } else {
4558 // The easy case: no previously existing features listed. We simply
4559 // set the OSNumber at key 'feature' and we're on our way.
4560 features->setObject(feature, new_feature_data.get());
4561 }
4562
4563 setProperty(kRootDomainSupportedFeatures, features.get());
4564
4565 if (featuresDictLock) {
4566 IOLockUnlock(featuresDictLock);
4567 }
4568
4569 // Notify EnergySaver and all those in user space so they might
4570 // re-populate their feature specific UI
4571 if (pmPowerStateQueue) {
4572 pmPowerStateQueue->submitPowerEvent( kPowerEventFeatureChanged );
4573 }
4574 }
4575
4576 //******************************************************************************
4577 // removePublishedFeature
4578 //
4579 // Removes previously published feature
4580 //******************************************************************************
4581
4582 IOReturn
removePublishedFeature(uint32_t removeFeatureID)4583 IOPMrootDomain::removePublishedFeature( uint32_t removeFeatureID )
4584 {
4585 IOReturn ret = kIOReturnError;
4586 uint32_t feature_value = 0;
4587 uint16_t feature_id = 0;
4588 bool madeAChange = false;
4589
4590 OSSymbol *dictKey = NULL;
4591 OSSharedPtr<OSCollectionIterator> dictIterator;
4592 OSArray *arrayMember = NULL;
4593 OSNumber *numberMember = NULL;
4594 OSObject *osObj = NULL;
4595 OSNumber *osNum = NULL;
4596 OSSharedPtr<OSArray> arrayMemberCopy;
4597
4598 if (kBadPMFeatureID == removeFeatureID) {
4599 return kIOReturnNotFound;
4600 }
4601
4602 if (featuresDictLock) {
4603 IOLockLock(featuresDictLock);
4604 }
4605
4606 OSSharedPtr<OSObject> origFeaturesProp = copyProperty(kRootDomainSupportedFeatures);
4607 OSDictionary *origFeatures = OSDynamicCast(OSDictionary, origFeaturesProp.get());
4608 OSSharedPtr<OSDictionary> features;
4609
4610 if (origFeatures) {
4611 // Any modifications to the dictionary are made to the copy to prevent
4612 // races & crashes with userland clients. Dictionary updated
4613 // automically later.
4614 features = OSDictionary::withDictionary(origFeatures);
4615 } else {
4616 features = NULL;
4617 ret = kIOReturnNotFound;
4618 goto exit;
4619 }
4620
4621 // We iterate 'features' dictionary looking for an entry tagged
4622 // with 'removeFeatureID'. If found, we remove it from our tracking
4623 // structures and notify the OS via a general interest message.
4624
4625 dictIterator = OSCollectionIterator::withCollection(features.get());
4626 if (!dictIterator) {
4627 goto exit;
4628 }
4629
4630 while ((dictKey = OSDynamicCast(OSSymbol, dictIterator->getNextObject()))) {
4631 osObj = features->getObject(dictKey);
4632
4633 // Each Feature is either tracked by an OSNumber
4634 if (osObj && (numberMember = OSDynamicCast(OSNumber, osObj))) {
4635 feature_value = numberMember->unsigned32BitValue();
4636 feature_id = (uint16_t)(feature_value >> 16);
4637
4638 if (feature_id == (uint16_t)removeFeatureID) {
4639 // Remove this node
4640 features->removeObject(dictKey);
4641 madeAChange = true;
4642 break;
4643 }
4644
4645 // Or tracked by an OSArray of OSNumbers
4646 } else if (osObj && (arrayMember = OSDynamicCast(OSArray, osObj))) {
4647 unsigned int arrayCount = arrayMember->getCount();
4648
4649 for (unsigned int i = 0; i < arrayCount; i++) {
4650 osNum = OSDynamicCast(OSNumber, arrayMember->getObject(i));
4651 if (!osNum) {
4652 continue;
4653 }
4654
4655 feature_value = osNum->unsigned32BitValue();
4656 feature_id = (uint16_t)(feature_value >> 16);
4657
4658 if (feature_id == (uint16_t)removeFeatureID) {
4659 // Remove this node
4660 if (1 == arrayCount) {
4661 // If the array only contains one element, remove
4662 // the whole thing.
4663 features->removeObject(dictKey);
4664 } else {
4665 // Otherwise remove the element from a copy of the array.
4666 arrayMemberCopy = OSArray::withArray(arrayMember);
4667 if (arrayMemberCopy) {
4668 arrayMemberCopy->removeObject(i);
4669 features->setObject(dictKey, arrayMemberCopy.get());
4670 }
4671 }
4672
4673 madeAChange = true;
4674 break;
4675 }
4676 }
4677 }
4678 }
4679
4680 if (madeAChange) {
4681 ret = kIOReturnSuccess;
4682
4683 setProperty(kRootDomainSupportedFeatures, features.get());
4684
4685 // Notify EnergySaver and all those in user space so they might
4686 // re-populate their feature specific UI
4687 if (pmPowerStateQueue) {
4688 pmPowerStateQueue->submitPowerEvent( kPowerEventFeatureChanged );
4689 }
4690 } else {
4691 ret = kIOReturnNotFound;
4692 }
4693
4694 exit:
4695 if (featuresDictLock) {
4696 IOLockUnlock(featuresDictLock);
4697 }
4698 return ret;
4699 }
4700
4701 //******************************************************************************
4702 // publishPMSetting (private)
4703 //
4704 // Should only be called by PMSettingObject to publish a PM Setting as a
4705 // supported feature.
4706 //******************************************************************************
4707
4708 void
publishPMSetting(const OSSymbol * feature,uint32_t where,uint32_t * featureID)4709 IOPMrootDomain::publishPMSetting(
4710 const OSSymbol * feature, uint32_t where, uint32_t * featureID )
4711 {
4712 if (noPublishPMSettings &&
4713 (noPublishPMSettings->getNextIndexOfObject(feature, 0) != (unsigned int)-1)) {
4714 // Setting found in noPublishPMSettings array
4715 *featureID = kBadPMFeatureID;
4716 return;
4717 }
4718
4719 publishFeature(
4720 feature->getCStringNoCopy(), where, featureID);
4721 }
4722
4723 //******************************************************************************
4724 // setPMSetting (private)
4725 //
4726 // Internal helper to relay PM settings changes from user space to individual
4727 // drivers. Should be called only by IOPMrootDomain::setProperties.
4728 //******************************************************************************
4729
4730 IOReturn
setPMSetting(const OSSymbol * type,OSObject * object)4731 IOPMrootDomain::setPMSetting(
4732 const OSSymbol *type,
4733 OSObject *object )
4734 {
4735 PMSettingCallEntry *entries = NULL;
4736 OSSharedPtr<OSArray> chosen;
4737 const OSArray *array;
4738 PMSettingObject *pmso;
4739 thread_t thisThread;
4740 int i, j, count, capacity;
4741 bool ok = false;
4742 IOReturn ret;
4743
4744 if (NULL == type) {
4745 return kIOReturnBadArgument;
4746 }
4747
4748 PMSETTING_LOCK();
4749
4750 // Update settings dict so changes are visible from copyPMSetting().
4751 fPMSettingsDict->setObject(type, object);
4752
4753 // Prep all PMSetting objects with the given 'type' for callout.
4754 array = OSDynamicCast(OSArray, settingsCallbacks->getObject(type));
4755 if (!array || ((capacity = array->getCount()) == 0)) {
4756 goto unlock_exit;
4757 }
4758
4759 // Array to retain PMSetting objects targeted for callout.
4760 chosen = OSArray::withCapacity(capacity);
4761 if (!chosen) {
4762 goto unlock_exit; // error
4763 }
4764 entries = IONew(PMSettingCallEntry, capacity);
4765 if (!entries) {
4766 goto unlock_exit; // error
4767 }
4768 memset(entries, 0, sizeof(PMSettingCallEntry) * capacity);
4769
4770 thisThread = current_thread();
4771
4772 for (i = 0, j = 0; i < capacity; i++) {
4773 pmso = (PMSettingObject *) array->getObject(i);
4774 if (pmso->disabled) {
4775 continue;
4776 }
4777 entries[j].thread = thisThread;
4778 queue_enter(&pmso->calloutQueue, &entries[j], PMSettingCallEntry *, link);
4779 chosen->setObject(pmso);
4780 j++;
4781 }
4782 count = j;
4783 if (!count) {
4784 goto unlock_exit;
4785 }
4786
4787 PMSETTING_UNLOCK();
4788
4789 // Call each pmso in the chosen array.
4790 for (i = 0; i < count; i++) {
4791 pmso = (PMSettingObject *) chosen->getObject(i);
4792 ret = pmso->dispatchPMSetting(type, object);
4793 if (ret == kIOReturnSuccess) {
4794 // At least one setting handler was successful
4795 ok = true;
4796 #if DEVELOPMENT || DEBUG
4797 } else {
4798 // Log the handler and kext that failed
4799 OSSharedPtr<const OSSymbol> kextName = copyKextIdentifierWithAddress((vm_address_t) pmso->func);
4800 if (kextName) {
4801 DLOG("PMSetting(%s) error 0x%x from %s\n",
4802 type->getCStringNoCopy(), ret, kextName->getCStringNoCopy());
4803 }
4804 #endif
4805 }
4806 }
4807
4808 PMSETTING_LOCK();
4809 for (i = 0; i < count; i++) {
4810 pmso = (PMSettingObject *) chosen->getObject(i);
4811 queue_remove(&pmso->calloutQueue, &entries[i], PMSettingCallEntry *, link);
4812 if (pmso->waitThread) {
4813 PMSETTING_WAKEUP(pmso);
4814 }
4815 }
4816
4817 if (ok) {
4818 recordRTCAlarm(type, object);
4819 }
4820 unlock_exit:
4821 PMSETTING_UNLOCK();
4822
4823 if (entries) {
4824 IODelete(entries, PMSettingCallEntry, capacity);
4825 }
4826
4827 return kIOReturnSuccess;
4828 }
4829
4830 //******************************************************************************
4831 // copyPMSetting (public)
4832 //
4833 // Allows kexts to safely read setting values, without being subscribed to
4834 // notifications.
4835 //******************************************************************************
4836
4837 OSSharedPtr<OSObject>
copyPMSetting(OSSymbol * whichSetting)4838 IOPMrootDomain::copyPMSetting(
4839 OSSymbol *whichSetting)
4840 {
4841 OSSharedPtr<OSObject> obj;
4842
4843 if (!whichSetting) {
4844 return NULL;
4845 }
4846
4847 PMSETTING_LOCK();
4848 obj.reset(fPMSettingsDict->getObject(whichSetting), OSRetain);
4849 PMSETTING_UNLOCK();
4850
4851 return obj;
4852 }
4853
4854 //******************************************************************************
4855 // registerPMSettingController (public)
4856 //
4857 // direct wrapper to registerPMSettingController with uint32_t power source arg
4858 //******************************************************************************
4859
4860 IOReturn
registerPMSettingController(const OSSymbol * settings[],IOPMSettingControllerCallback func,OSObject * target,uintptr_t refcon,OSObject ** handle)4861 IOPMrootDomain::registerPMSettingController(
4862 const OSSymbol * settings[],
4863 IOPMSettingControllerCallback func,
4864 OSObject *target,
4865 uintptr_t refcon,
4866 OSObject **handle)
4867 {
4868 return registerPMSettingController(
4869 settings,
4870 (kIOPMSupportedOnAC | kIOPMSupportedOnBatt | kIOPMSupportedOnUPS),
4871 func, target, refcon, handle);
4872 }
4873
4874 //******************************************************************************
4875 // registerPMSettingController (public)
4876 //
4877 // Kexts may register for notifications when a particular setting is changed.
4878 // A list of settings is available in IOPM.h.
4879 // Arguments:
4880 // * settings - An OSArray containing OSSymbols. Caller should populate this
4881 // array with a list of settings caller wants notifications from.
4882 // * func - A C function callback of the type IOPMSettingControllerCallback
4883 // * target - caller may provide an OSObject *, which PM will pass as an
4884 // target to calls to "func"
4885 // * refcon - caller may provide an void *, which PM will pass as an
4886 // argument to calls to "func"
4887 // * handle - This is a return argument. We will populate this pointer upon
4888 // call success. Hold onto this and pass this argument to
4889 // IOPMrootDomain::deRegisterPMSettingCallback when unloading your kext
4890 // Returns:
4891 // kIOReturnSuccess on success
4892 //******************************************************************************
4893
4894 IOReturn
registerPMSettingController(const OSSymbol * settings[],uint32_t supportedPowerSources,IOPMSettingControllerCallback func,OSObject * target,uintptr_t refcon,OSObject ** handle)4895 IOPMrootDomain::registerPMSettingController(
4896 const OSSymbol * settings[],
4897 uint32_t supportedPowerSources,
4898 IOPMSettingControllerCallback func,
4899 OSObject *target,
4900 uintptr_t refcon,
4901 OSObject **handle)
4902 {
4903 PMSettingObject *pmso = NULL;
4904 OSObject *pmsh = NULL;
4905 int i;
4906
4907 if (NULL == settings ||
4908 NULL == func ||
4909 NULL == handle) {
4910 return kIOReturnBadArgument;
4911 }
4912
4913 pmso = PMSettingObject::pmSettingObject(
4914 (IOPMrootDomain *) this, func, target,
4915 refcon, supportedPowerSources, settings, &pmsh);
4916
4917 if (!pmso) {
4918 *handle = NULL;
4919 return kIOReturnInternalError;
4920 }
4921
4922 PMSETTING_LOCK();
4923 for (i = 0; settings[i]; i++) {
4924 OSSharedPtr<OSArray> newList;
4925 OSArray *list = OSDynamicCast(OSArray, settingsCallbacks->getObject(settings[i]));
4926 if (!list) {
4927 // New array of callbacks for this setting
4928 newList = OSArray::withCapacity(1);
4929 settingsCallbacks->setObject(settings[i], newList.get());
4930 list = newList.get();
4931 }
4932
4933 // Add caller to the callback list
4934 list->setObject(pmso);
4935 }
4936 PMSETTING_UNLOCK();
4937
4938 // Return handle to the caller, the setting object is private.
4939 *handle = pmsh;
4940
4941 return kIOReturnSuccess;
4942 }
4943
4944 //******************************************************************************
4945 // deregisterPMSettingObject (private)
4946 //
4947 // Only called from PMSettingObject.
4948 //******************************************************************************
4949
4950 void
deregisterPMSettingObject(PMSettingObject * pmso)4951 IOPMrootDomain::deregisterPMSettingObject( PMSettingObject * pmso )
4952 {
4953 thread_t thisThread = current_thread();
4954 PMSettingCallEntry *callEntry;
4955 OSSharedPtr<OSCollectionIterator> iter;
4956 OSSymbol *sym;
4957 OSArray *array;
4958 int index;
4959 bool wait;
4960
4961 PMSETTING_LOCK();
4962
4963 pmso->disabled = true;
4964
4965 // Wait for all callout threads to finish.
4966 do {
4967 wait = false;
4968 queue_iterate(&pmso->calloutQueue, callEntry, PMSettingCallEntry *, link)
4969 {
4970 if (callEntry->thread != thisThread) {
4971 wait = true;
4972 break;
4973 }
4974 }
4975 if (wait) {
4976 assert(NULL == pmso->waitThread);
4977 pmso->waitThread = thisThread;
4978 PMSETTING_WAIT(pmso);
4979 pmso->waitThread = NULL;
4980 }
4981 } while (wait);
4982
4983 // Search each PM settings array in the kernel.
4984 iter = OSCollectionIterator::withCollection(settingsCallbacks.get());
4985 if (iter) {
4986 while ((sym = OSDynamicCast(OSSymbol, iter->getNextObject()))) {
4987 array = OSDynamicCast(OSArray, settingsCallbacks->getObject(sym));
4988 index = array->getNextIndexOfObject(pmso, 0);
4989 if (-1 != index) {
4990 array->removeObject(index);
4991 }
4992 }
4993 }
4994
4995 PMSETTING_UNLOCK();
4996
4997 pmso->release();
4998 }
4999
5000 //******************************************************************************
5001 // informCPUStateChange
5002 //
5003 // Call into PM CPU code so that CPU power savings may dynamically adjust for
5004 // running on battery, with the lid closed, etc.
5005 //
5006 // informCPUStateChange is a no-op on non x86 systems
5007 // only x86 has explicit support in the IntelCPUPowerManagement kext
5008 //******************************************************************************
5009
5010 void
informCPUStateChange(uint32_t type,uint32_t value)5011 IOPMrootDomain::informCPUStateChange(
5012 uint32_t type,
5013 uint32_t value )
5014 {
5015 #if defined(__i386__) || defined(__x86_64__)
5016
5017 pmioctlVariableInfo_t varInfoStruct;
5018 int pmCPUret = 0;
5019 const char *varNameStr = NULL;
5020 int32_t *varIndex = NULL;
5021
5022 if (kInformAC == type) {
5023 varNameStr = kIOPMRootDomainBatPowerCString;
5024 varIndex = &idxPMCPULimitedPower;
5025 } else if (kInformLid == type) {
5026 varNameStr = kIOPMRootDomainLidCloseCString;
5027 varIndex = &idxPMCPUClamshell;
5028 } else {
5029 return;
5030 }
5031
5032 // Set the new value!
5033 // pmCPUControl will assign us a new ID if one doesn't exist yet
5034 bzero(&varInfoStruct, sizeof(pmioctlVariableInfo_t));
5035 varInfoStruct.varID = *varIndex;
5036 varInfoStruct.varType = vBool;
5037 varInfoStruct.varInitValue = value;
5038 varInfoStruct.varCurValue = value;
5039 strlcpy((char *)varInfoStruct.varName,
5040 (const char *)varNameStr,
5041 sizeof(varInfoStruct.varName));
5042
5043 // Set!
5044 pmCPUret = pmCPUControl( PMIOCSETVARINFO, (void *)&varInfoStruct );
5045
5046 // pmCPU only assigns numerical id's when a new varName is specified
5047 if ((0 == pmCPUret)
5048 && (*varIndex == kCPUUnknownIndex)) {
5049 // pmCPUControl has assigned us a new variable ID.
5050 // Let's re-read the structure we just SET to learn that ID.
5051 pmCPUret = pmCPUControl( PMIOCGETVARNAMEINFO, (void *)&varInfoStruct );
5052
5053 if (0 == pmCPUret) {
5054 // Store it in idxPMCPUClamshell or idxPMCPULimitedPower
5055 *varIndex = varInfoStruct.varID;
5056 }
5057 }
5058
5059 return;
5060
5061 #endif /* __i386__ || __x86_64__ */
5062 }
5063
5064 // MARK: -
5065 // MARK: Deep Sleep Policy
5066
5067 #if HIBERNATION
5068
5069 //******************************************************************************
5070 // evaluateSystemSleepPolicy
5071 //******************************************************************************
5072
5073 #define kIOPlatformSystemSleepPolicyKey "IOPlatformSystemSleepPolicy"
5074
5075 // Sleep flags
5076 enum {
5077 kIOPMSleepFlagHibernate = 0x00000001,
5078 kIOPMSleepFlagSleepTimerEnable = 0x00000002
5079 };
5080
5081 struct IOPMSystemSleepPolicyEntry {
5082 uint32_t factorMask;
5083 uint32_t factorBits;
5084 uint32_t sleepFlags;
5085 uint32_t wakeEvents;
5086 } __attribute__((packed));
5087
5088 struct IOPMSystemSleepPolicyTable {
5089 uint32_t signature;
5090 uint16_t version;
5091 uint16_t entryCount;
5092 IOPMSystemSleepPolicyEntry entries[];
5093 } __attribute__((packed));
5094
5095 enum {
5096 kIOPMSleepAttributeHibernateSetup = 0x00000001,
5097 kIOPMSleepAttributeHibernateSleep = 0x00000002
5098 };
5099
5100 static uint32_t
getSleepTypeAttributes(uint32_t sleepType)5101 getSleepTypeAttributes( uint32_t sleepType )
5102 {
5103 static const uint32_t sleepTypeAttributes[kIOPMSleepTypeLast] =
5104 {
5105 /* invalid */ 0,
5106 /* abort */ 0,
5107 /* normal */ 0,
5108 /* safesleep */ kIOPMSleepAttributeHibernateSetup,
5109 /* hibernate */ kIOPMSleepAttributeHibernateSetup | kIOPMSleepAttributeHibernateSleep,
5110 /* standby */ kIOPMSleepAttributeHibernateSetup | kIOPMSleepAttributeHibernateSleep,
5111 /* poweroff */ kIOPMSleepAttributeHibernateSetup | kIOPMSleepAttributeHibernateSleep,
5112 /* deepidle */ 0
5113 };
5114
5115 if (sleepType >= kIOPMSleepTypeLast) {
5116 return 0;
5117 }
5118
5119 return sleepTypeAttributes[sleepType];
5120 }
5121
5122 bool
evaluateSystemSleepPolicy(IOPMSystemSleepParameters * params,int sleepPhase,uint32_t * hibMode)5123 IOPMrootDomain::evaluateSystemSleepPolicy(
5124 IOPMSystemSleepParameters * params, int sleepPhase, uint32_t * hibMode )
5125 {
5126 #define SLEEP_FACTOR(x) {(uint32_t) kIOPMSleepFactor ## x, #x}
5127
5128 static const IONamedValue factorValues[] = {
5129 SLEEP_FACTOR( SleepTimerWake ),
5130 SLEEP_FACTOR( LidOpen ),
5131 SLEEP_FACTOR( ACPower ),
5132 SLEEP_FACTOR( BatteryLow ),
5133 SLEEP_FACTOR( StandbyNoDelay ),
5134 SLEEP_FACTOR( StandbyForced ),
5135 SLEEP_FACTOR( StandbyDisabled ),
5136 SLEEP_FACTOR( USBExternalDevice ),
5137 SLEEP_FACTOR( BluetoothHIDDevice ),
5138 SLEEP_FACTOR( ExternalMediaMounted ),
5139 SLEEP_FACTOR( ThunderboltDevice ),
5140 SLEEP_FACTOR( RTCAlarmScheduled ),
5141 SLEEP_FACTOR( MagicPacketWakeEnabled ),
5142 SLEEP_FACTOR( HibernateForced ),
5143 SLEEP_FACTOR( AutoPowerOffDisabled ),
5144 SLEEP_FACTOR( AutoPowerOffForced ),
5145 SLEEP_FACTOR( ExternalDisplay ),
5146 SLEEP_FACTOR( NetworkKeepAliveActive ),
5147 SLEEP_FACTOR( LocalUserActivity ),
5148 SLEEP_FACTOR( HibernateFailed ),
5149 SLEEP_FACTOR( ThermalWarning ),
5150 SLEEP_FACTOR( DisplayCaptured ),
5151 { 0, NULL }
5152 };
5153
5154 const IOPMSystemSleepPolicyTable * pt;
5155 OSSharedPtr<OSObject> prop;
5156 OSData * policyData;
5157 uint64_t currentFactors = 0;
5158 char currentFactorsBuf[512];
5159 uint32_t standbyDelay = 0;
5160 uint32_t powerOffDelay = 0;
5161 uint32_t powerOffTimer = 0;
5162 uint32_t standbyTimer = 0;
5163 uint32_t mismatch;
5164 bool standbyEnabled;
5165 bool powerOffEnabled;
5166 bool found = false;
5167
5168 // Get platform's sleep policy table
5169 if (!gSleepPolicyHandler) {
5170 prop = getServiceRoot()->copyProperty(kIOPlatformSystemSleepPolicyKey);
5171 if (!prop) {
5172 goto done;
5173 }
5174 }
5175
5176 // Fetch additional settings
5177 standbyEnabled = (getSleepOption(kIOPMDeepSleepDelayKey, &standbyDelay)
5178 && propertyHasValue(kIOPMDeepSleepEnabledKey, kOSBooleanTrue));
5179 powerOffEnabled = (getSleepOption(kIOPMAutoPowerOffDelayKey, &powerOffDelay)
5180 && propertyHasValue(kIOPMAutoPowerOffEnabledKey, kOSBooleanTrue));
5181 if (!getSleepOption(kIOPMAutoPowerOffTimerKey, &powerOffTimer)) {
5182 powerOffTimer = powerOffDelay;
5183 }
5184 if (!getSleepOption(kIOPMDeepSleepTimerKey, &standbyTimer)) {
5185 standbyTimer = standbyDelay;
5186 }
5187
5188 DLOG("phase %d, standby %d delay %u timer %u, poweroff %d delay %u timer %u, hibernate 0x%x\n",
5189 sleepPhase, standbyEnabled, standbyDelay, standbyTimer,
5190 powerOffEnabled, powerOffDelay, powerOffTimer, *hibMode);
5191
5192 currentFactorsBuf[0] = 0;
5193 // pmset level overrides
5194 if ((*hibMode & kIOHibernateModeOn) == 0) {
5195 if (!gSleepPolicyHandler) {
5196 standbyEnabled = false;
5197 powerOffEnabled = false;
5198 }
5199 } else if (!(*hibMode & kIOHibernateModeSleep)) {
5200 // Force hibernate (i.e. mode 25)
5201 // If standby is enabled, force standy.
5202 // If poweroff is enabled, force poweroff.
5203 if (standbyEnabled) {
5204 currentFactors |= kIOPMSleepFactorStandbyForced;
5205 } else if (powerOffEnabled) {
5206 currentFactors |= kIOPMSleepFactorAutoPowerOffForced;
5207 } else {
5208 currentFactors |= kIOPMSleepFactorHibernateForced;
5209 }
5210 }
5211
5212 // Current factors based on environment and assertions
5213 if (sleepTimerMaintenance) {
5214 currentFactors |= kIOPMSleepFactorSleepTimerWake;
5215 }
5216 if (standbyEnabled && sleepToStandby && !gSleepPolicyHandler) {
5217 currentFactors |= kIOPMSleepFactorSleepTimerWake;
5218 }
5219 if (!clamshellClosed) {
5220 currentFactors |= kIOPMSleepFactorLidOpen;
5221 }
5222 if (acAdaptorConnected) {
5223 currentFactors |= kIOPMSleepFactorACPower;
5224 }
5225 if (lowBatteryCondition) {
5226 hibernateMode = 0;
5227 getSleepOption(kIOHibernateModeKey, &hibernateMode);
5228 if ((hibernateMode & kIOHibernateModeOn) == 0) {
5229 DLOG("HibernateMode is 0. Not sending LowBattery factor to IOPPF\n");
5230 } else {
5231 currentFactors |= kIOPMSleepFactorBatteryLow;
5232 }
5233 }
5234 if (!standbyDelay || !standbyTimer) {
5235 currentFactors |= kIOPMSleepFactorStandbyNoDelay;
5236 }
5237 if (standbyNixed || !standbyEnabled) {
5238 currentFactors |= kIOPMSleepFactorStandbyDisabled;
5239 }
5240 if (resetTimers) {
5241 currentFactors |= kIOPMSleepFactorLocalUserActivity;
5242 currentFactors &= ~kIOPMSleepFactorSleepTimerWake;
5243 }
5244 if (getPMAssertionLevel(kIOPMDriverAssertionUSBExternalDeviceBit) !=
5245 kIOPMDriverAssertionLevelOff) {
5246 currentFactors |= kIOPMSleepFactorUSBExternalDevice;
5247 }
5248 if (getPMAssertionLevel(kIOPMDriverAssertionBluetoothHIDDevicePairedBit) !=
5249 kIOPMDriverAssertionLevelOff) {
5250 currentFactors |= kIOPMSleepFactorBluetoothHIDDevice;
5251 }
5252 if (getPMAssertionLevel(kIOPMDriverAssertionExternalMediaMountedBit) !=
5253 kIOPMDriverAssertionLevelOff) {
5254 currentFactors |= kIOPMSleepFactorExternalMediaMounted;
5255 }
5256 if (getPMAssertionLevel(kIOPMDriverAssertionReservedBit5) !=
5257 kIOPMDriverAssertionLevelOff) {
5258 currentFactors |= kIOPMSleepFactorThunderboltDevice;
5259 }
5260 if (_scheduledAlarmMask != 0) {
5261 currentFactors |= kIOPMSleepFactorRTCAlarmScheduled;
5262 }
5263 if (getPMAssertionLevel(kIOPMDriverAssertionMagicPacketWakeEnabledBit) !=
5264 kIOPMDriverAssertionLevelOff) {
5265 currentFactors |= kIOPMSleepFactorMagicPacketWakeEnabled;
5266 }
5267 #define TCPKEEPALIVE 1
5268 #if TCPKEEPALIVE
5269 if (getPMAssertionLevel(kIOPMDriverAssertionNetworkKeepAliveActiveBit) !=
5270 kIOPMDriverAssertionLevelOff) {
5271 currentFactors |= kIOPMSleepFactorNetworkKeepAliveActive;
5272 }
5273 #endif
5274 if (!powerOffEnabled) {
5275 currentFactors |= kIOPMSleepFactorAutoPowerOffDisabled;
5276 }
5277 if (desktopMode) {
5278 currentFactors |= kIOPMSleepFactorExternalDisplay;
5279 }
5280 if (userWasActive) {
5281 currentFactors |= kIOPMSleepFactorLocalUserActivity;
5282 }
5283 if (darkWakeHibernateError && !CAP_HIGHEST(kIOPMSystemCapabilityGraphics)) {
5284 currentFactors |= kIOPMSleepFactorHibernateFailed;
5285 }
5286 if (thermalWarningState) {
5287 currentFactors |= kIOPMSleepFactorThermalWarning;
5288 }
5289
5290 for (int factorBit = 0; factorBit < (8 * sizeof(uint32_t)); factorBit++) {
5291 uint32_t factor = 1 << factorBit;
5292 if (factor & currentFactors) {
5293 strlcat(currentFactorsBuf, ", ", sizeof(currentFactorsBuf));
5294 strlcat(currentFactorsBuf, IOFindNameForValue(factor, factorValues), sizeof(currentFactorsBuf));
5295 }
5296 }
5297 DLOG("sleep factors 0x%llx%s\n", currentFactors, currentFactorsBuf);
5298
5299 if (gSleepPolicyHandler) {
5300 uint32_t savedHibernateMode;
5301 IOReturn result;
5302
5303 if (!gSleepPolicyVars) {
5304 gSleepPolicyVars = IOMallocType(IOPMSystemSleepPolicyVariables);
5305 }
5306 gSleepPolicyVars->signature = kIOPMSystemSleepPolicySignature;
5307 gSleepPolicyVars->version = kIOPMSystemSleepPolicyVersion;
5308 gSleepPolicyVars->currentCapability = _currentCapability;
5309 gSleepPolicyVars->highestCapability = _highestCapability;
5310 gSleepPolicyVars->sleepFactors = currentFactors;
5311 gSleepPolicyVars->sleepReason = lastSleepReason;
5312 gSleepPolicyVars->sleepPhase = sleepPhase;
5313 gSleepPolicyVars->standbyDelay = standbyDelay;
5314 gSleepPolicyVars->standbyTimer = standbyTimer;
5315 gSleepPolicyVars->poweroffDelay = powerOffDelay;
5316 gSleepPolicyVars->scheduledAlarms = _scheduledAlarmMask | _userScheduledAlarmMask;
5317 gSleepPolicyVars->poweroffTimer = powerOffTimer;
5318
5319 if (kIOPMSleepPhase0 == sleepPhase) {
5320 // preserve hibernateMode
5321 savedHibernateMode = gSleepPolicyVars->hibernateMode;
5322 gSleepPolicyVars->hibernateMode = *hibMode;
5323 } else if (kIOPMSleepPhase1 == sleepPhase) {
5324 // use original hibernateMode for phase2
5325 gSleepPolicyVars->hibernateMode = *hibMode;
5326 }
5327
5328 result = gSleepPolicyHandler(gSleepPolicyTarget, gSleepPolicyVars, params);
5329
5330 if (kIOPMSleepPhase0 == sleepPhase) {
5331 // restore hibernateMode
5332 gSleepPolicyVars->hibernateMode = savedHibernateMode;
5333 }
5334
5335 if ((result != kIOReturnSuccess) ||
5336 (kIOPMSleepTypeInvalid == params->sleepType) ||
5337 (params->sleepType >= kIOPMSleepTypeLast) ||
5338 (kIOPMSystemSleepParametersVersion != params->version)) {
5339 MSG("sleep policy handler error\n");
5340 goto done;
5341 }
5342
5343 if ((getSleepTypeAttributes(params->sleepType) &
5344 kIOPMSleepAttributeHibernateSetup) &&
5345 ((*hibMode & kIOHibernateModeOn) == 0)) {
5346 *hibMode |= (kIOHibernateModeOn | kIOHibernateModeSleep);
5347 }
5348
5349 DLOG("sleep params v%u, type %u, flags 0x%x, wake 0x%x, timer %u, poweroff %u\n",
5350 params->version, params->sleepType, params->sleepFlags,
5351 params->ecWakeEvents, params->ecWakeTimer, params->ecPoweroffTimer);
5352 found = true;
5353 goto done;
5354 }
5355
5356 // Policy table is meaningless without standby enabled
5357 if (!standbyEnabled) {
5358 goto done;
5359 }
5360
5361 // Validate the sleep policy table
5362 policyData = OSDynamicCast(OSData, prop.get());
5363 if (!policyData || (policyData->getLength() <= sizeof(IOPMSystemSleepPolicyTable))) {
5364 goto done;
5365 }
5366
5367 pt = (const IOPMSystemSleepPolicyTable *) policyData->getBytesNoCopy();
5368 if ((pt->signature != kIOPMSystemSleepPolicySignature) ||
5369 (pt->version != 1) || (0 == pt->entryCount)) {
5370 goto done;
5371 }
5372
5373 if (((policyData->getLength() - sizeof(IOPMSystemSleepPolicyTable)) !=
5374 (sizeof(IOPMSystemSleepPolicyEntry) * pt->entryCount))) {
5375 goto done;
5376 }
5377
5378 for (uint32_t i = 0; i < pt->entryCount; i++) {
5379 const IOPMSystemSleepPolicyEntry * entry = &pt->entries[i];
5380 mismatch = (((uint32_t)currentFactors ^ entry->factorBits) & entry->factorMask);
5381
5382 DLOG("mask 0x%08x, bits 0x%08x, flags 0x%08x, wake 0x%08x, mismatch 0x%08x\n",
5383 entry->factorMask, entry->factorBits,
5384 entry->sleepFlags, entry->wakeEvents, mismatch);
5385 if (mismatch) {
5386 continue;
5387 }
5388
5389 DLOG("^ found match\n");
5390 found = true;
5391
5392 params->version = kIOPMSystemSleepParametersVersion;
5393 params->reserved1 = 1;
5394 if (entry->sleepFlags & kIOPMSleepFlagHibernate) {
5395 params->sleepType = kIOPMSleepTypeStandby;
5396 } else {
5397 params->sleepType = kIOPMSleepTypeNormalSleep;
5398 }
5399
5400 params->ecWakeEvents = entry->wakeEvents;
5401 if (entry->sleepFlags & kIOPMSleepFlagSleepTimerEnable) {
5402 if (kIOPMSleepPhase2 == sleepPhase) {
5403 clock_sec_t now_secs = gIOLastSleepTime.tv_sec;
5404
5405 if (!_standbyTimerResetSeconds ||
5406 (now_secs <= _standbyTimerResetSeconds)) {
5407 // Reset standby timer adjustment
5408 _standbyTimerResetSeconds = now_secs;
5409 DLOG("standby delay %u, reset %u\n",
5410 standbyDelay, (uint32_t) _standbyTimerResetSeconds);
5411 } else if (standbyDelay) {
5412 // Shorten the standby delay timer
5413 clock_sec_t elapsed = now_secs - _standbyTimerResetSeconds;
5414 if (standbyDelay > elapsed) {
5415 standbyDelay -= elapsed;
5416 } else {
5417 standbyDelay = 1; // must be > 0
5418 }
5419 DLOG("standby delay %u, elapsed %u\n",
5420 standbyDelay, (uint32_t) elapsed);
5421 }
5422 }
5423 params->ecWakeTimer = standbyDelay;
5424 } else if (kIOPMSleepPhase2 == sleepPhase) {
5425 // A sleep that does not enable the sleep timer will reset
5426 // the standby delay adjustment.
5427 _standbyTimerResetSeconds = 0;
5428 }
5429 break;
5430 }
5431
5432 done:
5433 return found;
5434 }
5435
5436 static IOPMSystemSleepParameters gEarlySystemSleepParams;
5437
5438 void
evaluateSystemSleepPolicyEarly(void)5439 IOPMrootDomain::evaluateSystemSleepPolicyEarly( void )
5440 {
5441 // Evaluate early (priority interest phase), before drivers sleep.
5442
5443 DLOG("%s\n", __FUNCTION__);
5444 removeProperty(kIOPMSystemSleepParametersKey);
5445
5446 // Full wake resets the standby timer delay adjustment
5447 if (_highestCapability & kIOPMSystemCapabilityGraphics) {
5448 _standbyTimerResetSeconds = 0;
5449 }
5450
5451 hibernateDisabled = false;
5452 hibernateMode = 0;
5453 getSleepOption(kIOHibernateModeKey, &hibernateMode);
5454
5455 // Save for late evaluation if sleep is aborted
5456 bzero(&gEarlySystemSleepParams, sizeof(gEarlySystemSleepParams));
5457
5458 if (evaluateSystemSleepPolicy(&gEarlySystemSleepParams, kIOPMSleepPhase1,
5459 &hibernateMode)) {
5460 if (!hibernateRetry &&
5461 ((getSleepTypeAttributes(gEarlySystemSleepParams.sleepType) &
5462 kIOPMSleepAttributeHibernateSetup) == 0)) {
5463 // skip hibernate setup
5464 hibernateDisabled = true;
5465 }
5466 }
5467
5468 // Publish IOPMSystemSleepType
5469 uint32_t sleepType = gEarlySystemSleepParams.sleepType;
5470 if (sleepType == kIOPMSleepTypeInvalid) {
5471 // no sleep policy
5472 sleepType = kIOPMSleepTypeNormalSleep;
5473 if (hibernateMode & kIOHibernateModeOn) {
5474 sleepType = (hibernateMode & kIOHibernateModeSleep) ?
5475 kIOPMSleepTypeSafeSleep : kIOPMSleepTypeHibernate;
5476 }
5477 } else if ((sleepType == kIOPMSleepTypeStandby) &&
5478 (gEarlySystemSleepParams.ecPoweroffTimer)) {
5479 // report the lowest possible sleep state
5480 sleepType = kIOPMSleepTypePowerOff;
5481 }
5482
5483 setProperty(kIOPMSystemSleepTypeKey, sleepType, 32);
5484 }
5485
5486 void
evaluateSystemSleepPolicyFinal(void)5487 IOPMrootDomain::evaluateSystemSleepPolicyFinal( void )
5488 {
5489 IOPMSystemSleepParameters params;
5490 OSSharedPtr<OSData> paramsData;
5491 bool wakeNow;
5492 // Evaluate sleep policy after sleeping drivers but before platform sleep.
5493
5494 DLOG("%s\n", __FUNCTION__);
5495
5496 bzero(¶ms, sizeof(params));
5497 wakeNow = false;
5498 if (evaluateSystemSleepPolicy(¶ms, kIOPMSleepPhase2, &hibernateMode)) {
5499 if ((kIOPMSleepTypeStandby == params.sleepType)
5500 && gIOHibernateStandbyDisabled && gSleepPolicyVars
5501 && (!((kIOPMSleepFactorStandbyForced | kIOPMSleepFactorAutoPowerOffForced | kIOPMSleepFactorHibernateForced)
5502 & gSleepPolicyVars->sleepFactors))) {
5503 standbyNixed = true;
5504 wakeNow = true;
5505 }
5506 if (wakeNow
5507 || ((hibernateDisabled || hibernateAborted) &&
5508 (getSleepTypeAttributes(params.sleepType) &
5509 kIOPMSleepAttributeHibernateSetup))) {
5510 // Final evaluation picked a state requiring hibernation,
5511 // but hibernate isn't going to proceed. Arm a short sleep using
5512 // the early non-hibernate sleep parameters.
5513 bcopy(&gEarlySystemSleepParams, ¶ms, sizeof(params));
5514 params.sleepType = kIOPMSleepTypeAbortedSleep;
5515 params.ecWakeTimer = 1;
5516 if (standbyNixed) {
5517 resetTimers = true;
5518 } else {
5519 // Set hibernateRetry flag to force hibernate setup on the
5520 // next sleep.
5521 hibernateRetry = true;
5522 }
5523 DLOG("wake in %u secs for hibernateDisabled %d, hibernateAborted %d, standbyNixed %d\n",
5524 params.ecWakeTimer, hibernateDisabled, hibernateAborted, standbyNixed);
5525 } else {
5526 hibernateRetry = false;
5527 }
5528
5529 if (kIOPMSleepTypeAbortedSleep != params.sleepType) {
5530 resetTimers = false;
5531 }
5532
5533 paramsData = OSData::withValue(params);
5534 if (paramsData) {
5535 setProperty(kIOPMSystemSleepParametersKey, paramsData.get());
5536 }
5537
5538 if (getSleepTypeAttributes(params.sleepType) &
5539 kIOPMSleepAttributeHibernateSleep) {
5540 // Disable sleep to force hibernation
5541 gIOHibernateMode &= ~kIOHibernateModeSleep;
5542 }
5543 }
5544 }
5545
5546 bool
getHibernateSettings(uint32_t * hibernateModePtr,uint32_t * hibernateFreeRatio,uint32_t * hibernateFreeTime)5547 IOPMrootDomain::getHibernateSettings(
5548 uint32_t * hibernateModePtr,
5549 uint32_t * hibernateFreeRatio,
5550 uint32_t * hibernateFreeTime )
5551 {
5552 // Called by IOHibernateSystemSleep() after evaluateSystemSleepPolicyEarly()
5553 // has updated the hibernateDisabled flag.
5554
5555 bool ok = getSleepOption(kIOHibernateModeKey, hibernateModePtr);
5556 getSleepOption(kIOHibernateFreeRatioKey, hibernateFreeRatio);
5557 getSleepOption(kIOHibernateFreeTimeKey, hibernateFreeTime);
5558 if (hibernateDisabled) {
5559 *hibernateModePtr = 0;
5560 } else if (gSleepPolicyHandler) {
5561 *hibernateModePtr = hibernateMode;
5562 }
5563 DLOG("hibernateMode 0x%x\n", *hibernateModePtr);
5564 return ok;
5565 }
5566
5567 bool
getSleepOption(const char * key,uint32_t * option)5568 IOPMrootDomain::getSleepOption( const char * key, uint32_t * option )
5569 {
5570 OSSharedPtr<OSObject> optionsProp;
5571 OSDictionary * optionsDict;
5572 OSSharedPtr<OSObject> obj;
5573 OSNumber * num;
5574 bool ok = false;
5575
5576 optionsProp = copyProperty(kRootDomainSleepOptionsKey);
5577 optionsDict = OSDynamicCast(OSDictionary, optionsProp.get());
5578
5579 if (optionsDict) {
5580 obj.reset(optionsDict->getObject(key), OSRetain);
5581 }
5582 if (!obj) {
5583 obj = copyProperty(key);
5584 }
5585 if (obj) {
5586 if ((num = OSDynamicCast(OSNumber, obj.get()))) {
5587 *option = num->unsigned32BitValue();
5588 ok = true;
5589 } else if (OSDynamicCast(OSBoolean, obj.get())) {
5590 *option = (obj == kOSBooleanTrue) ? 1 : 0;
5591 ok = true;
5592 }
5593 }
5594
5595 return ok;
5596 }
5597 #endif /* HIBERNATION */
5598
5599 IOReturn
getSystemSleepType(uint32_t * sleepType,uint32_t * standbyTimer)5600 IOPMrootDomain::getSystemSleepType( uint32_t * sleepType, uint32_t * standbyTimer )
5601 {
5602 #if HIBERNATION
5603 IOPMSystemSleepParameters params;
5604 uint32_t hibMode = 0;
5605 bool ok;
5606
5607 if (gIOPMWorkLoop->inGate() == false) {
5608 IOReturn ret = gIOPMWorkLoop->runAction(
5609 OSMemberFunctionCast(IOWorkLoop::Action, this,
5610 &IOPMrootDomain::getSystemSleepType),
5611 (OSObject *) this,
5612 (void *) sleepType, (void *) standbyTimer);
5613 return ret;
5614 }
5615
5616 getSleepOption(kIOHibernateModeKey, &hibMode);
5617 bzero(¶ms, sizeof(params));
5618
5619 ok = evaluateSystemSleepPolicy(¶ms, kIOPMSleepPhase0, &hibMode);
5620 if (ok) {
5621 *sleepType = params.sleepType;
5622 if (!getSleepOption(kIOPMDeepSleepTimerKey, standbyTimer) &&
5623 !getSleepOption(kIOPMDeepSleepDelayKey, standbyTimer)) {
5624 DLOG("Standby delay is not set\n");
5625 *standbyTimer = 0;
5626 }
5627 return kIOReturnSuccess;
5628 }
5629 #endif
5630
5631 return kIOReturnUnsupported;
5632 }
5633
5634 // MARK: -
5635 // MARK: Shutdown and Restart
5636
5637 //******************************************************************************
5638 // handlePlatformHaltRestart
5639 //
5640 //******************************************************************************
5641
5642 // Phases while performing shutdown/restart
5643 typedef enum {
5644 kNotifyDone = 0x00,
5645 kNotifyPriorityClients = 0x10,
5646 kNotifyPowerPlaneDrivers = 0x20,
5647 kNotifyHaltRestartAction = 0x30,
5648 kQuiescePM = 0x40,
5649 } shutdownPhase_t;
5650
5651
5652 struct HaltRestartApplierContext {
5653 IOPMrootDomain * RootDomain;
5654 unsigned long PowerState;
5655 IOPMPowerFlags PowerFlags;
5656 UInt32 MessageType;
5657 UInt32 Counter;
5658 const char * LogString;
5659 shutdownPhase_t phase;
5660
5661 IOServiceInterestHandler handler;
5662 } gHaltRestartCtx;
5663
5664 const char *
shutdownPhase2String(shutdownPhase_t phase)5665 shutdownPhase2String(shutdownPhase_t phase)
5666 {
5667 switch (phase) {
5668 case kNotifyDone:
5669 return "Notifications completed";
5670 case kNotifyPriorityClients:
5671 return "Notifying priority clients";
5672 case kNotifyPowerPlaneDrivers:
5673 return "Notifying power plane drivers";
5674 case kNotifyHaltRestartAction:
5675 return "Notifying HaltRestart action handlers";
5676 case kQuiescePM:
5677 return "Quiescing PM";
5678 default:
5679 return "Unknown";
5680 }
5681 }
5682
5683 static void
platformHaltRestartApplier(OSObject * object,void * context)5684 platformHaltRestartApplier( OSObject * object, void * context )
5685 {
5686 IOPowerStateChangeNotification notify;
5687 HaltRestartApplierContext * ctx;
5688 AbsoluteTime startTime, elapsedTime;
5689 uint32_t deltaTime;
5690
5691 ctx = (HaltRestartApplierContext *) context;
5692
5693 _IOServiceInterestNotifier * notifier;
5694 notifier = OSDynamicCast(_IOServiceInterestNotifier, object);
5695 memset(¬ify, 0, sizeof(notify));
5696 notify.powerRef = (void *)(uintptr_t)ctx->Counter;
5697 notify.returnValue = 0;
5698 notify.stateNumber = ctx->PowerState;
5699 notify.stateFlags = ctx->PowerFlags;
5700
5701 if (notifier) {
5702 ctx->handler = notifier->handler;
5703 }
5704
5705 clock_get_uptime(&startTime);
5706 ctx->RootDomain->messageClient( ctx->MessageType, object, (void *)¬ify );
5707 deltaTime = computeDeltaTimeMS(&startTime, &elapsedTime);
5708
5709 if ((deltaTime > kPMHaltTimeoutMS) && notifier) {
5710 LOG("%s handler %p took %u ms\n",
5711 ctx->LogString, OBFUSCATE(notifier->handler), deltaTime);
5712 halt_log_enter("PowerOff/Restart message to priority client", (const void *) notifier->handler, elapsedTime);
5713 }
5714
5715 ctx->handler = NULL;
5716 ctx->Counter++;
5717 }
5718
5719 static void
quiescePowerTreeCallback(void * target,void * param)5720 quiescePowerTreeCallback( void * target, void * param )
5721 {
5722 IOLockLock(gPMHaltLock);
5723 gPMQuiesced = true;
5724 thread_wakeup(param);
5725 IOLockUnlock(gPMHaltLock);
5726 }
5727
5728 void
handlePlatformHaltRestart(UInt32 pe_type)5729 IOPMrootDomain::handlePlatformHaltRestart( UInt32 pe_type )
5730 {
5731 AbsoluteTime startTime, elapsedTime;
5732 uint32_t deltaTime;
5733 bool nvramSync = false;
5734
5735 memset(&gHaltRestartCtx, 0, sizeof(gHaltRestartCtx));
5736 gHaltRestartCtx.RootDomain = this;
5737
5738 clock_get_uptime(&startTime);
5739 switch (pe_type) {
5740 case kPEHaltCPU:
5741 case kPEUPSDelayHaltCPU:
5742 gHaltRestartCtx.PowerState = OFF_STATE;
5743 gHaltRestartCtx.MessageType = kIOMessageSystemWillPowerOff;
5744 gHaltRestartCtx.LogString = "PowerOff";
5745 nvramSync = true;
5746 break;
5747
5748 case kPERestartCPU:
5749 gHaltRestartCtx.PowerState = RESTART_STATE;
5750 gHaltRestartCtx.MessageType = kIOMessageSystemWillRestart;
5751 gHaltRestartCtx.LogString = "Restart";
5752 nvramSync = true;
5753 break;
5754
5755 case kPEPagingOff:
5756 gHaltRestartCtx.PowerState = ON_STATE;
5757 gHaltRestartCtx.MessageType = kIOMessageSystemPagingOff;
5758 gHaltRestartCtx.LogString = "PagingOff";
5759 IOService::updateConsoleUsers(NULL, kIOMessageSystemPagingOff);
5760 #if HIBERNATION
5761 IOHibernateSystemRestart();
5762 #endif
5763 break;
5764
5765 default:
5766 return;
5767 }
5768
5769 if (nvramSync) {
5770 PESyncNVRAM();
5771 }
5772
5773 gHaltRestartCtx.phase = kNotifyPriorityClients;
5774 // Notify legacy clients
5775 applyToInterested(gIOPriorityPowerStateInterest, platformHaltRestartApplier, &gHaltRestartCtx);
5776
5777 // For normal shutdown, turn off File Server Mode.
5778 if (kPEHaltCPU == pe_type) {
5779 OSSharedPtr<const OSSymbol> setting = OSSymbol::withCString(kIOPMSettingRestartOnPowerLossKey);
5780 OSSharedPtr<OSNumber> num = OSNumber::withNumber((unsigned long long) 0, 32);
5781 if (setting && num) {
5782 setPMSetting(setting.get(), num.get());
5783 }
5784 }
5785
5786 if (kPEPagingOff != pe_type) {
5787 gHaltRestartCtx.phase = kNotifyPowerPlaneDrivers;
5788 // Notify in power tree order
5789 notifySystemShutdown(this, gHaltRestartCtx.MessageType);
5790 }
5791
5792 gHaltRestartCtx.phase = kNotifyHaltRestartAction;
5793 #if defined(XNU_TARGET_OS_OSX)
5794 IOCPURunPlatformHaltRestartActions(pe_type);
5795 #else /* !defined(XNU_TARGET_OS_OSX) */
5796 if (kPEPagingOff != pe_type) {
5797 IOCPURunPlatformHaltRestartActions(pe_type);
5798 }
5799 #endif /* !defined(XNU_TARGET_OS_OSX) */
5800
5801 // Wait for PM to quiesce
5802 if ((kPEPagingOff != pe_type) && gPMHaltLock) {
5803 gHaltRestartCtx.phase = kQuiescePM;
5804 AbsoluteTime quiesceTime = mach_absolute_time();
5805
5806 IOLockLock(gPMHaltLock);
5807 gPMQuiesced = false;
5808 if (quiescePowerTree(this, &quiescePowerTreeCallback, &gPMQuiesced) ==
5809 kIOReturnSuccess) {
5810 while (!gPMQuiesced) {
5811 IOLockSleep(gPMHaltLock, &gPMQuiesced, THREAD_UNINT);
5812 }
5813 }
5814 IOLockUnlock(gPMHaltLock);
5815 deltaTime = computeDeltaTimeMS(&quiesceTime, &elapsedTime);
5816 DLOG("PM quiesce took %u ms\n", deltaTime);
5817 halt_log_enter("Quiesce", NULL, elapsedTime);
5818 }
5819 gHaltRestartCtx.phase = kNotifyDone;
5820
5821 deltaTime = computeDeltaTimeMS(&startTime, &elapsedTime);
5822 LOG("%s all drivers took %u ms\n", gHaltRestartCtx.LogString, deltaTime);
5823
5824 halt_log_enter(gHaltRestartCtx.LogString, NULL, elapsedTime);
5825
5826 deltaTime = computeDeltaTimeMS(&gHaltStartTime, &elapsedTime);
5827 LOG("%s total %u ms\n", gHaltRestartCtx.LogString, deltaTime);
5828
5829 if (gHaltLog && gHaltTimeMaxLog && (deltaTime >= gHaltTimeMaxLog)) {
5830 printf("%s total %d ms:%s\n", gHaltRestartCtx.LogString, deltaTime, gHaltLog);
5831 }
5832
5833 checkShutdownTimeout();
5834 }
5835
5836 bool
checkShutdownTimeout()5837 IOPMrootDomain::checkShutdownTimeout()
5838 {
5839 AbsoluteTime elapsedTime;
5840 uint32_t deltaTime = computeDeltaTimeMS(&gHaltStartTime, &elapsedTime);
5841
5842 if (gHaltTimeMaxPanic && (deltaTime >= gHaltTimeMaxPanic)) {
5843 return true;
5844 }
5845 return false;
5846 }
5847
5848 void
panicWithShutdownLog(uint32_t timeoutInMs)5849 IOPMrootDomain::panicWithShutdownLog(uint32_t timeoutInMs)
5850 {
5851 if (gHaltLog) {
5852 if ((gHaltRestartCtx.phase == kNotifyPriorityClients) && gHaltRestartCtx.handler) {
5853 halt_log_enter("Blocked on priority client", (void *)gHaltRestartCtx.handler, mach_absolute_time() - gHaltStartTime);
5854 }
5855 panic("%s timed out in phase '%s'. Total %d ms:%s",
5856 gHaltRestartCtx.LogString, shutdownPhase2String(gHaltRestartCtx.phase), timeoutInMs, gHaltLog);
5857 } else {
5858 panic("%s timed out in phase \'%s\'. Total %d ms",
5859 gHaltRestartCtx.LogString, shutdownPhase2String(gHaltRestartCtx.phase), timeoutInMs);
5860 }
5861 }
5862
5863 //******************************************************************************
5864 // shutdownSystem
5865 //
5866 //******************************************************************************
5867
5868 IOReturn
shutdownSystem(void)5869 IOPMrootDomain::shutdownSystem( void )
5870 {
5871 return kIOReturnUnsupported;
5872 }
5873
5874 //******************************************************************************
5875 // restartSystem
5876 //
5877 //******************************************************************************
5878
5879 IOReturn
restartSystem(void)5880 IOPMrootDomain::restartSystem( void )
5881 {
5882 return kIOReturnUnsupported;
5883 }
5884
5885 // MARK: -
5886 // MARK: System Capability
5887
5888 //******************************************************************************
5889 // tagPowerPlaneService
5890 //
5891 // Running on PM work loop thread.
5892 //******************************************************************************
5893
5894 void
tagPowerPlaneService(IOService * service,IOPMActions * actions,IOPMPowerStateIndex maxPowerState)5895 IOPMrootDomain::tagPowerPlaneService(
5896 IOService * service,
5897 IOPMActions * actions,
5898 IOPMPowerStateIndex maxPowerState )
5899 {
5900 uint32_t flags = 0;
5901
5902 memset(actions, 0, sizeof(*actions));
5903 actions->target = this;
5904
5905 if (service == this) {
5906 actions->actionPowerChangeStart =
5907 OSMemberFunctionCast(
5908 IOPMActionPowerChangeStart, this,
5909 &IOPMrootDomain::handleOurPowerChangeStart);
5910
5911 actions->actionPowerChangeDone =
5912 OSMemberFunctionCast(
5913 IOPMActionPowerChangeDone, this,
5914 &IOPMrootDomain::handleOurPowerChangeDone);
5915
5916 actions->actionPowerChangeOverride =
5917 OSMemberFunctionCast(
5918 IOPMActionPowerChangeOverride, this,
5919 &IOPMrootDomain::overrideOurPowerChange);
5920 return;
5921 }
5922
5923 #if DISPLAY_WRANGLER_PRESENT
5924 if (NULL != service->metaCast("IODisplayWrangler")) {
5925 // XXX should this really retain?
5926 wrangler.reset(service, OSRetain);
5927 wrangler->registerInterest(gIOGeneralInterest,
5928 &displayWranglerNotification, this, NULL);
5929
5930 // found the display wrangler, check for any display assertions already created
5931 if (pmAssertions->getActivatedAssertions() & kIOPMDriverAssertionPreventDisplaySleepBit) {
5932 DLOG("wrangler setIgnoreIdleTimer\(1) due to pre-existing assertion\n");
5933 wrangler->setIgnoreIdleTimer( true );
5934 }
5935 flags |= kPMActionsFlagIsDisplayWrangler;
5936 }
5937 #endif /* DISPLAY_WRANGLER_PRESENT */
5938
5939 if (service->propertyExists("IOPMStrictTreeOrder")) {
5940 flags |= kPMActionsFlagIsGraphicsDriver;
5941 }
5942 if (service->propertyExists("IOPMUnattendedWakePowerState")) {
5943 flags |= kPMActionsFlagIsAudioDriver;
5944 }
5945
5946 // Find the power connection object that is a child of the PCI host
5947 // bridge, and has a graphics/audio device attached below. Mark the
5948 // power branch for delayed child notifications.
5949
5950 if (flags) {
5951 IORegistryEntry * child = service;
5952 IORegistryEntry * parent = child->getParentEntry(gIOPowerPlane);
5953
5954 while (child != this) {
5955 if (child->propertyHasValue("IOPCITunnelled", kOSBooleanTrue)) {
5956 // Skip delaying notifications and clamping power on external graphics and audio devices.
5957 DLOG("Avoiding delayChildNotification on object 0x%llx. flags: 0x%x\n", service->getRegistryEntryID(), flags);
5958 flags = 0;
5959 break;
5960 }
5961 if ((parent == pciHostBridgeDriver) ||
5962 (parent == this)) {
5963 if (OSDynamicCast(IOPowerConnection, child)) {
5964 IOPowerConnection * conn = (IOPowerConnection *) child;
5965 conn->delayChildNotification = true;
5966 DLOG("delayChildNotification for 0x%llx\n", conn->getRegistryEntryID());
5967 }
5968 break;
5969 }
5970 child = parent;
5971 parent = child->getParentEntry(gIOPowerPlane);
5972 }
5973 }
5974
5975 OSSharedPtr<OSObject> prop = service->copyProperty(kIOPMDarkWakeMaxPowerStateKey);
5976 if (prop) {
5977 OSNumber * num = OSDynamicCast(OSNumber, prop.get());
5978 if (num) {
5979 actions->darkWakePowerState = num->unsigned32BitValue();
5980 if (actions->darkWakePowerState < maxPowerState) {
5981 flags |= kPMActionsFlagHasDarkWakePowerState;
5982 }
5983 }
5984 }
5985
5986
5987 if (flags) {
5988 DLOG("%s tag flags %x\n", service->getName(), flags);
5989 actions->flags |= flags;
5990 actions->actionPowerChangeOverride =
5991 OSMemberFunctionCast(
5992 IOPMActionPowerChangeOverride, this,
5993 &IOPMrootDomain::overridePowerChangeForService);
5994
5995 if (flags & kPMActionsFlagIsDisplayWrangler) {
5996 actions->actionActivityTickle =
5997 OSMemberFunctionCast(
5998 IOPMActionActivityTickle, this,
5999 &IOPMrootDomain::handleActivityTickleForDisplayWrangler);
6000
6001 actions->actionUpdatePowerClient =
6002 OSMemberFunctionCast(
6003 IOPMActionUpdatePowerClient, this,
6004 &IOPMrootDomain::handleUpdatePowerClientForDisplayWrangler);
6005 }
6006 return;
6007 }
6008
6009 // Locate the first PCI host bridge for PMTrace.
6010 if (!pciHostBridgeDevice && service->metaCast("IOPCIBridge")) {
6011 IOService * provider = service->getProvider();
6012 if (OSDynamicCast(IOPlatformDevice, provider) &&
6013 provider->inPlane(gIODTPlane)) {
6014 pciHostBridgeDevice.reset(provider, OSNoRetain);
6015 pciHostBridgeDriver.reset(service, OSNoRetain);
6016 DLOG("PMTrace found PCI host bridge %s->%s\n",
6017 provider->getName(), service->getName());
6018 }
6019 }
6020
6021 // Tag top-level PCI devices. The order of PMinit() call does not
6022 // change across boots and is used as the PCI bit number.
6023 if (pciHostBridgeDevice && service->metaCast("IOPCIDevice")) {
6024 // Would prefer to check built-in property, but tagPowerPlaneService()
6025 // is called before pciDevice->registerService().
6026 IORegistryEntry * parent = service->getParentEntry(gIODTPlane);
6027 if ((parent == pciHostBridgeDevice) && service->propertyExists("acpi-device")) {
6028 int bit = pmTracer->recordTopLevelPCIDevice( service );
6029 if (bit >= 0) {
6030 // Save the assigned bit for fast lookup.
6031 actions->flags |= (bit & kPMActionsPCIBitNumberMask);
6032
6033 actions->actionPowerChangeStart =
6034 OSMemberFunctionCast(
6035 IOPMActionPowerChangeStart, this,
6036 &IOPMrootDomain::handlePowerChangeStartForPCIDevice);
6037
6038 actions->actionPowerChangeDone =
6039 OSMemberFunctionCast(
6040 IOPMActionPowerChangeDone, this,
6041 &IOPMrootDomain::handlePowerChangeDoneForPCIDevice);
6042 }
6043 }
6044 }
6045 }
6046
6047 //******************************************************************************
6048 // PM actions for root domain
6049 //******************************************************************************
6050
6051 void
overrideOurPowerChange(IOService * service,IOPMActions * actions,const IOPMRequest * request,IOPMPowerStateIndex * inOutPowerState,IOPMPowerChangeFlags * inOutChangeFlags)6052 IOPMrootDomain::overrideOurPowerChange(
6053 IOService * service,
6054 IOPMActions * actions,
6055 const IOPMRequest * request,
6056 IOPMPowerStateIndex * inOutPowerState,
6057 IOPMPowerChangeFlags * inOutChangeFlags )
6058 {
6059 uint32_t changeFlags = *inOutChangeFlags;
6060 uint32_t desiredPowerState = (uint32_t) *inOutPowerState;
6061 uint32_t currentPowerState = (uint32_t) getPowerState();
6062
6063 if (request->getTag() == 0) {
6064 // Set a tag for any request that originates from IOServicePM
6065 (const_cast<IOPMRequest *>(request))->fTag = nextRequestTag(kCPSReasonPMInternals);
6066 }
6067
6068 DLOG("PowerChangeOverride (%s->%s, %x, 0x%x) tag 0x%x\n",
6069 getPowerStateString(currentPowerState),
6070 getPowerStateString(desiredPowerState),
6071 _currentCapability, changeFlags,
6072 request->getTag());
6073
6074
6075 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
6076 /*
6077 * ASBM send lowBattery notifications every 1 second until the device
6078 * enters hibernation. This queues up multiple sleep requests.
6079 * After the device wakes from hibernation, none of these previously
6080 * queued sleep requests are valid.
6081 * lowBattteryCondition variable is set when ASBM notifies rootDomain
6082 * and is cleared at the very last point in sleep.
6083 * Any attempt to sleep with reason kIOPMSleepReasonLowPower without
6084 * lowBatteryCondition is invalid
6085 */
6086 if (REQUEST_TAG_TO_REASON(request->getTag()) == kIOPMSleepReasonLowPower) {
6087 if (!lowBatteryCondition) {
6088 DLOG("Duplicate lowBattery sleep");
6089 *inOutChangeFlags |= kIOPMNotDone;
6090 return;
6091 }
6092 }
6093 #endif
6094
6095 if ((AOT_STATE == desiredPowerState) && (ON_STATE == currentPowerState)) {
6096 // Assertion may have been taken in AOT leading to changePowerStateTo(AOT)
6097 *inOutChangeFlags |= kIOPMNotDone;
6098 return;
6099 }
6100
6101 if (changeFlags & kIOPMParentInitiated) {
6102 // Root parent is permanently pegged at max power,
6103 // a parent initiated power change is unexpected.
6104 *inOutChangeFlags |= kIOPMNotDone;
6105 return;
6106 }
6107
6108 if (desiredPowerState < currentPowerState) {
6109 if (CAP_CURRENT(kIOPMSystemCapabilityGraphics)) {
6110 // Root domain is dropping power state from ON->SLEEP.
6111 // If system is in full wake, first enter dark wake by
6112 // converting the power drop to a capability change.
6113 // Once in dark wake, transition to sleep state ASAP.
6114
6115 darkWakeToSleepASAP = true;
6116
6117 // Drop graphics and audio capability
6118 _desiredCapability &= ~(
6119 kIOPMSystemCapabilityGraphics |
6120 kIOPMSystemCapabilityAudio);
6121
6122 // Convert to capability change (ON->ON)
6123 *inOutPowerState = getRUN_STATE();
6124 *inOutChangeFlags |= kIOPMSynchronize;
6125
6126 // Revert device desire from SLEEP to ON
6127 changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonPowerOverride);
6128 } else {
6129 // System is already in dark wake, ok to drop power state.
6130 // Broadcast root power down to entire tree.
6131 *inOutChangeFlags |= kIOPMRootChangeDown;
6132 }
6133 } else if (desiredPowerState > currentPowerState) {
6134 if ((_currentCapability & kIOPMSystemCapabilityCPU) == 0) {
6135 // Broadcast power up when waking from sleep, but not for the
6136 // initial power change at boot by checking for cpu capability.
6137 *inOutChangeFlags |= kIOPMRootChangeUp;
6138 }
6139 }
6140 }
6141
6142 void
handleOurPowerChangeStart(IOService * service,IOPMActions * actions,const IOPMRequest * request,IOPMPowerStateIndex newPowerState,IOPMPowerChangeFlags * inOutChangeFlags)6143 IOPMrootDomain::handleOurPowerChangeStart(
6144 IOService * service,
6145 IOPMActions * actions,
6146 const IOPMRequest * request,
6147 IOPMPowerStateIndex newPowerState,
6148 IOPMPowerChangeFlags * inOutChangeFlags )
6149 {
6150 IOPMRequestTag requestTag = request->getTag();
6151 IOPMRequestTag sleepReason;
6152
6153 uint32_t changeFlags = *inOutChangeFlags;
6154 uint32_t currentPowerState = (uint32_t) getPowerState();
6155 bool publishSleepReason = false;
6156
6157 // Check if request has a valid sleep reason
6158 sleepReason = REQUEST_TAG_TO_REASON(requestTag);
6159 if (sleepReason < kIOPMSleepReasonClamshell) {
6160 sleepReason = kIOPMSleepReasonIdle;
6161 }
6162
6163 _systemTransitionType = kSystemTransitionNone;
6164 _systemMessageClientMask = 0;
6165 capabilityLoss = false;
6166 toldPowerdCapWillChange = false;
6167
6168 // Emergency notifications may arrive after the initial sleep request
6169 // has been queued. Override the sleep reason so powerd and others can
6170 // treat this as an emergency sleep.
6171 if (lowBatteryCondition) {
6172 sleepReason = kIOPMSleepReasonLowPower;
6173 } else if (thermalEmergencyState) {
6174 sleepReason = kIOPMSleepReasonThermalEmergency;
6175 }
6176
6177 // 1. Explicit capability change.
6178 if (changeFlags & kIOPMSynchronize) {
6179 if (newPowerState == ON_STATE) {
6180 if (changeFlags & kIOPMSyncNoChildNotify) {
6181 _systemTransitionType = kSystemTransitionNewCapClient;
6182 } else {
6183 _systemTransitionType = kSystemTransitionCapability;
6184 }
6185 }
6186 }
6187 // 2. Going to sleep (cancellation still possible).
6188 else if (newPowerState < currentPowerState) {
6189 _systemTransitionType = kSystemTransitionSleep;
6190 }
6191 // 3. Woke from (idle or demand) sleep.
6192 else if (!systemBooting &&
6193 (changeFlags & kIOPMSelfInitiated) &&
6194 (newPowerState > currentPowerState)) {
6195 _systemTransitionType = kSystemTransitionWake;
6196 _desiredCapability = kIOPMSystemCapabilityCPU | kIOPMSystemCapabilityNetwork;
6197
6198 // Early exit from dark wake to full (e.g. LID open)
6199 if (kFullWakeReasonNone != fullWakeReason) {
6200 _desiredCapability |= (
6201 kIOPMSystemCapabilityGraphics |
6202 kIOPMSystemCapabilityAudio);
6203
6204 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
6205 if (fullWakeReason == kFullWakeReasonLocalUser) {
6206 darkWakeExit = true;
6207 darkWakeToSleepASAP = false;
6208 setProperty(kIOPMRootDomainWakeTypeKey, isRTCAlarmWake ?
6209 kIOPMRootDomainWakeTypeAlarm : kIOPMRootDomainWakeTypeUser);
6210 }
6211 #endif
6212 }
6213 #if HIBERNATION
6214 IOHibernateSetWakeCapabilities(_desiredCapability);
6215 #endif
6216 }
6217
6218 // Update pending wake capability at the beginning of every
6219 // state transition (including synchronize). This will become
6220 // the current capability at the end of the transition.
6221
6222 if (kSystemTransitionSleep == _systemTransitionType) {
6223 _pendingCapability = 0;
6224 capabilityLoss = true;
6225 } else if (kSystemTransitionNewCapClient != _systemTransitionType) {
6226 _pendingCapability = _desiredCapability |
6227 kIOPMSystemCapabilityCPU |
6228 kIOPMSystemCapabilityNetwork;
6229
6230 if (_pendingCapability & kIOPMSystemCapabilityGraphics) {
6231 _pendingCapability |= kIOPMSystemCapabilityAudio;
6232 }
6233
6234 if ((kSystemTransitionCapability == _systemTransitionType) &&
6235 (_pendingCapability == _currentCapability)) {
6236 // Cancel the PM state change.
6237 _systemTransitionType = kSystemTransitionNone;
6238 *inOutChangeFlags |= kIOPMNotDone;
6239 }
6240 if (__builtin_popcount(_pendingCapability) <
6241 __builtin_popcount(_currentCapability)) {
6242 capabilityLoss = true;
6243 }
6244 }
6245
6246 // 1. Capability change.
6247 if (kSystemTransitionCapability == _systemTransitionType) {
6248 // Dark to Full transition.
6249 if (CAP_GAIN(kIOPMSystemCapabilityGraphics)) {
6250 tracePoint( kIOPMTracePointDarkWakeExit );
6251
6252 #if defined(XNU_TARGET_OS_OSX)
6253 // rdar://problem/65627936
6254 // When a dark->full wake promotion is scheduled before an ON->SLEEP
6255 // power state drop, invalidate any request to drop power state already
6256 // in the queue, including the override variant, unless full wake cannot
6257 // be sustained. Any power state drop queued after this SustainFullWake
6258 // request will not be affected.
6259 if (checkSystemCanSustainFullWake()) {
6260 changePowerStateWithOverrideTo(getRUN_STATE(), kCPSReasonSustainFullWake);
6261 }
6262 #endif
6263
6264 willEnterFullWake();
6265 }
6266
6267 // Full to Dark transition.
6268 if (CAP_LOSS(kIOPMSystemCapabilityGraphics)) {
6269 // Clear previous stats
6270 IOLockLock(pmStatsLock);
6271 if (pmStatsAppResponses) {
6272 pmStatsAppResponses = OSArray::withCapacity(5);
6273 }
6274 IOLockUnlock(pmStatsLock);
6275
6276 tracePoint( kIOPMTracePointDarkWakeEntry );
6277 *inOutChangeFlags |= kIOPMSyncTellPowerDown;
6278 _systemMessageClientMask = kSystemMessageClientPowerd |
6279 kSystemMessageClientLegacyApp;
6280
6281 // rdar://15971327
6282 // Prevent user active transitions before notifying clients
6283 // that system will sleep.
6284 preventTransitionToUserActive(true);
6285
6286 IOService::setAdvisoryTickleEnable( false );
6287
6288 // Publish the sleep reason for full to dark wake
6289 publishSleepReason = true;
6290 lastSleepReason = fullToDarkReason = sleepReason;
6291
6292 // Publish a UUID for the Sleep --> Wake cycle
6293 handlePublishSleepWakeUUID(true);
6294 if (sleepDelaysReport) {
6295 clock_get_uptime(&ts_sleepStart);
6296 DLOG("sleepDelaysReport f->9 start at 0x%llx\n", ts_sleepStart);
6297 }
6298
6299 darkWakeExit = false;
6300 }
6301 }
6302 // 2. System sleep.
6303 else if (kSystemTransitionSleep == _systemTransitionType) {
6304 // Beginning of a system sleep transition.
6305 // Cancellation is still possible.
6306 tracePoint( kIOPMTracePointSleepStarted );
6307
6308 _systemMessageClientMask = kSystemMessageClientAll;
6309 if ((_currentCapability & kIOPMSystemCapabilityGraphics) == 0) {
6310 _systemMessageClientMask &= ~kSystemMessageClientLegacyApp;
6311 }
6312 if ((_highestCapability & kIOPMSystemCapabilityGraphics) == 0) {
6313 // Kernel priority clients are only notified on the initial
6314 // transition to full wake, so don't notify them unless system
6315 // has gained graphics capability since the last system wake.
6316 _systemMessageClientMask &= ~kSystemMessageClientKernel;
6317 } else {
6318 // System was in full wake, but the downwards power transition is driven
6319 // by a request that originates from IOServicePM, so it isn't tagged with
6320 // a valid system sleep reason.
6321 if (REQUEST_TAG_TO_REASON(requestTag) == kCPSReasonPMInternals) {
6322 // Publish the same reason for full to dark
6323 sleepReason = fullToDarkReason;
6324 }
6325 }
6326 #if HIBERNATION
6327 gIOHibernateState = 0;
6328 #endif
6329
6330 // Record the reason for dark wake back to sleep
6331 // System may not have ever achieved full wake
6332
6333 publishSleepReason = true;
6334 lastSleepReason = sleepReason;
6335 if (sleepDelaysReport) {
6336 clock_get_uptime(&ts_sleepStart);
6337 DLOG("sleepDelaysReport 9->0 start at 0x%llx\n", ts_sleepStart);
6338 }
6339 }
6340 // 3. System wake.
6341 else if (kSystemTransitionWake == _systemTransitionType) {
6342 tracePoint( kIOPMTracePointWakeWillPowerOnClients );
6343 // Clear stats about sleep
6344
6345 if (AOT_STATE == newPowerState) {
6346 _pendingCapability = 0;
6347 }
6348
6349 if (AOT_STATE == currentPowerState) {
6350 // Wake events are no longer accepted after waking to AOT_STATE.
6351 // Re-enable wake event acceptance to append wake events claimed
6352 // during the AOT to ON_STATE transition.
6353 acceptSystemWakeEvents(kAcceptSystemWakeEvents_Reenable);
6354 }
6355
6356 if (_pendingCapability & kIOPMSystemCapabilityGraphics) {
6357 willEnterFullWake();
6358 }
6359 }
6360
6361 // The only location where the sleep reason is published. At this point
6362 // sleep can still be cancelled, but sleep reason should be published
6363 // early for logging purposes.
6364
6365 if (publishSleepReason) {
6366 static const char * IOPMSleepReasons[] =
6367 {
6368 kIOPMClamshellSleepKey,
6369 kIOPMPowerButtonSleepKey,
6370 kIOPMSoftwareSleepKey,
6371 kIOPMOSSwitchHibernationKey,
6372 kIOPMIdleSleepKey,
6373 kIOPMLowPowerSleepKey,
6374 kIOPMThermalEmergencySleepKey,
6375 kIOPMMaintenanceSleepKey,
6376 kIOPMSleepServiceExitKey,
6377 kIOPMDarkWakeThermalEmergencyKey,
6378 kIOPMNotificationWakeExitKey
6379 };
6380
6381 // Record sleep cause in IORegistry
6382 uint32_t reasonIndex = sleepReason - kIOPMSleepReasonClamshell;
6383 if (reasonIndex < sizeof(IOPMSleepReasons) / sizeof(IOPMSleepReasons[0])) {
6384 DLOG("sleep reason %s\n", IOPMSleepReasons[reasonIndex]);
6385 setProperty(kRootDomainSleepReasonKey, IOPMSleepReasons[reasonIndex]);
6386 }
6387 }
6388
6389 if ((kSystemTransitionNone != _systemTransitionType) &&
6390 (kSystemTransitionNewCapClient != _systemTransitionType)) {
6391 _systemStateGeneration++;
6392 systemDarkWake = false;
6393
6394 DLOG("=== START (%s->%s, %x->%x, 0x%x) gen %u, msg %x, tag %x\n",
6395 getPowerStateString(currentPowerState),
6396 getPowerStateString((uint32_t) newPowerState),
6397 _currentCapability, _pendingCapability,
6398 *inOutChangeFlags, _systemStateGeneration, _systemMessageClientMask,
6399 requestTag);
6400 }
6401
6402 if ((AOT_STATE == newPowerState) && (SLEEP_STATE != currentPowerState)) {
6403 panic("illegal AOT entry from %s", getPowerStateString(currentPowerState));
6404 }
6405 if (_aotNow && (ON_STATE == newPowerState)) {
6406 WAKEEVENT_LOCK();
6407 aotShouldExit(false, true);
6408 WAKEEVENT_UNLOCK();
6409 aotExit(false);
6410 }
6411 }
6412
6413 void
handleOurPowerChangeDone(IOService * service,IOPMActions * actions,const IOPMRequest * request,IOPMPowerStateIndex oldPowerState,IOPMPowerChangeFlags changeFlags)6414 IOPMrootDomain::handleOurPowerChangeDone(
6415 IOService * service,
6416 IOPMActions * actions,
6417 const IOPMRequest * request,
6418 IOPMPowerStateIndex oldPowerState,
6419 IOPMPowerChangeFlags changeFlags )
6420 {
6421 if (kSystemTransitionNewCapClient == _systemTransitionType) {
6422 _systemTransitionType = kSystemTransitionNone;
6423 return;
6424 }
6425
6426 if (_systemTransitionType != kSystemTransitionNone) {
6427 uint32_t currentPowerState = (uint32_t) getPowerState();
6428
6429 if (changeFlags & kIOPMNotDone) {
6430 // Power down was cancelled or vetoed.
6431 _pendingCapability = _currentCapability;
6432 lastSleepReason = 0;
6433
6434 // When sleep is cancelled or reverted, don't report
6435 // the target (lower) power state as the previous state.
6436 oldPowerState = currentPowerState;
6437
6438 if (!CAP_CURRENT(kIOPMSystemCapabilityGraphics) &&
6439 CAP_CURRENT(kIOPMSystemCapabilityCPU)) {
6440 #if defined(XNU_TARGET_OS_OSX)
6441 pmPowerStateQueue->submitPowerEvent(
6442 kPowerEventPolicyStimulus,
6443 (void *) kStimulusDarkWakeReentry,
6444 _systemStateGeneration );
6445 #else /* !defined(XNU_TARGET_OS_OSX) */
6446 // On embedded, there are no factors that can prolong a
6447 // "darkWake" when a power down is vetoed. We need to
6448 // promote to "fullWake" at least once so that factors
6449 // that prevent idle sleep can assert themselves if required
6450 pmPowerStateQueue->submitPowerEvent(
6451 kPowerEventPolicyStimulus,
6452 (void *) kStimulusDarkWakeActivityTickle);
6453 #endif /* !defined(XNU_TARGET_OS_OSX) */
6454 }
6455
6456 // Revert device desire to max.
6457 changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonPowerDownCancel);
6458 } else {
6459 // Send message on dark wake to full wake promotion.
6460 // tellChangeUp() handles the normal SLEEP->ON case.
6461
6462 if (kSystemTransitionCapability == _systemTransitionType) {
6463 if (CAP_GAIN(kIOPMSystemCapabilityGraphics)) {
6464 lastSleepReason = 0; // stop logging wrangler tickles
6465 tellClients(kIOMessageSystemHasPoweredOn);
6466 }
6467 if (CAP_LOSS(kIOPMSystemCapabilityGraphics)) {
6468 // Going dark, reset full wake state
6469 // userIsActive will be cleared by wrangler powering down
6470 fullWakeReason = kFullWakeReasonNone;
6471
6472 if (ts_sleepStart) {
6473 clock_get_uptime(&wake2DarkwakeDelay);
6474 SUB_ABSOLUTETIME(&wake2DarkwakeDelay, &ts_sleepStart);
6475 DLOG("sleepDelaysReport f->9 end 0x%llx\n", wake2DarkwakeDelay);
6476 ts_sleepStart = 0;
6477 }
6478 }
6479 }
6480
6481 // Reset state after exiting from dark wake.
6482
6483 if (CAP_GAIN(kIOPMSystemCapabilityGraphics) ||
6484 CAP_LOSS(kIOPMSystemCapabilityCPU)) {
6485 darkWakeMaintenance = false;
6486 darkWakeToSleepASAP = false;
6487 pciCantSleepValid = false;
6488 darkWakeSleepService = false;
6489
6490 if (CAP_LOSS(kIOPMSystemCapabilityCPU)) {
6491 // Remove the influence of display power assertion
6492 // before next system wake.
6493 if (wrangler) {
6494 wrangler->changePowerStateForRootDomain(
6495 kWranglerPowerStateMin );
6496 }
6497 removeProperty(gIOPMUserTriggeredFullWakeKey.get());
6498 }
6499 }
6500
6501 // Entered dark mode.
6502
6503 if (((_pendingCapability & kIOPMSystemCapabilityGraphics) == 0) &&
6504 (_pendingCapability & kIOPMSystemCapabilityCPU)) {
6505 // Queue an evaluation of whether to remain in dark wake,
6506 // and for how long. This serves the purpose of draining
6507 // any assertions from the queue.
6508
6509 pmPowerStateQueue->submitPowerEvent(
6510 kPowerEventPolicyStimulus,
6511 (void *) kStimulusDarkWakeEntry,
6512 _systemStateGeneration );
6513 }
6514 }
6515
6516 DLOG("=== FINISH (%s->%s, %x->%x, 0x%x) gen %u, msg %x, tag %x\n",
6517 getPowerStateString((uint32_t) oldPowerState), getPowerStateString(currentPowerState),
6518 _currentCapability, _pendingCapability,
6519 changeFlags, _systemStateGeneration, _systemMessageClientMask,
6520 request->getTag());
6521
6522 if ((currentPowerState == ON_STATE) && pmAssertions) {
6523 pmAssertions->reportCPUBitAccounting();
6524 }
6525
6526 if (_pendingCapability & kIOPMSystemCapabilityGraphics) {
6527 displayWakeCnt++;
6528 #if DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY
6529 if (clamshellExists && fullWakeThreadCall) {
6530 AbsoluteTime deadline;
6531 clock_interval_to_deadline(DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY, kSecondScale, &deadline);
6532 thread_call_enter_delayed(fullWakeThreadCall, deadline);
6533 }
6534 #endif
6535 } else if (CAP_GAIN(kIOPMSystemCapabilityCPU)) {
6536 darkWakeCnt++;
6537 }
6538
6539 // Update current system capability.
6540 if (_currentCapability != _pendingCapability) {
6541 _currentCapability = _pendingCapability;
6542 }
6543
6544 // Update highest system capability.
6545
6546 _highestCapability |= _currentCapability;
6547
6548 if (darkWakePostTickle &&
6549 (kSystemTransitionWake == _systemTransitionType) &&
6550 (gDarkWakeFlags & kDarkWakeFlagPromotionMask) ==
6551 kDarkWakeFlagPromotionLate) {
6552 darkWakePostTickle = false;
6553 reportUserInput();
6554 } else if (darkWakeExit) {
6555 requestFullWake( kFullWakeReasonLocalUser );
6556 }
6557
6558 // Reset tracepoint at completion of capability change,
6559 // completion of wake transition, and aborted sleep transition.
6560
6561 if ((_systemTransitionType == kSystemTransitionCapability) ||
6562 (_systemTransitionType == kSystemTransitionWake) ||
6563 ((_systemTransitionType == kSystemTransitionSleep) &&
6564 (changeFlags & kIOPMNotDone))) {
6565 setProperty(kIOPMSystemCapabilitiesKey, _currentCapability, 64);
6566 tracePoint( kIOPMTracePointSystemUp );
6567 }
6568
6569 _systemTransitionType = kSystemTransitionNone;
6570 _systemMessageClientMask = 0;
6571 toldPowerdCapWillChange = false;
6572
6573 darkWakeLogClamp = false;
6574
6575 if (lowBatteryCondition) {
6576 privateSleepSystem(kIOPMSleepReasonLowPower);
6577 } else if (thermalEmergencyState) {
6578 privateSleepSystem(kIOPMSleepReasonThermalEmergency);
6579 } else if ((fullWakeReason == kFullWakeReasonDisplayOn) && !displayPowerOnRequested) {
6580 // Request for full wake is removed while system is waking up to full wake
6581 DLOG("DisplayOn fullwake request is removed\n");
6582 handleSetDisplayPowerOn(false);
6583 }
6584
6585 if ((gClamshellFlags & kClamshell_WAR_47715679) && isRTCAlarmWake) {
6586 pmPowerStateQueue->submitPowerEvent(
6587 kPowerEventReceivedPowerNotification, (void *)(uintptr_t) kLocalEvalClamshellCommand );
6588 }
6589 }
6590 }
6591
6592 //******************************************************************************
6593 // PM actions for graphics and audio.
6594 //******************************************************************************
6595
6596 void
overridePowerChangeForService(IOService * service,IOPMActions * actions,const IOPMRequest * request,IOPMPowerStateIndex * inOutPowerState,IOPMPowerChangeFlags * inOutChangeFlags)6597 IOPMrootDomain::overridePowerChangeForService(
6598 IOService * service,
6599 IOPMActions * actions,
6600 const IOPMRequest * request,
6601 IOPMPowerStateIndex * inOutPowerState,
6602 IOPMPowerChangeFlags * inOutChangeFlags )
6603 {
6604 uint32_t powerState = (uint32_t) *inOutPowerState;
6605 uint32_t changeFlags = (uint32_t) *inOutChangeFlags;
6606 const uint32_t actionFlags = actions->flags;
6607
6608 if (kSystemTransitionNone == _systemTransitionType) {
6609 // Not in midst of a system transition.
6610 // Do not set kPMActionsStatePowerClamped.
6611 } else if ((actions->state & kPMActionsStatePowerClamped) == 0) {
6612 bool enableClamp = false;
6613
6614 // For most drivers, enable the clamp during ON->Dark transition
6615 // which has the kIOPMSynchronize flag set in changeFlags.
6616 if ((actionFlags & kPMActionsFlagIsDisplayWrangler) &&
6617 ((_pendingCapability & kIOPMSystemCapabilityGraphics) == 0) &&
6618 (changeFlags & kIOPMSynchronize)) {
6619 enableClamp = true;
6620 } else if ((actionFlags & kPMActionsFlagIsAudioDriver) &&
6621 ((gDarkWakeFlags & kDarkWakeFlagAudioNotSuppressed) == 0) &&
6622 ((_pendingCapability & kIOPMSystemCapabilityAudio) == 0) &&
6623 (changeFlags & kIOPMSynchronize)) {
6624 enableClamp = true;
6625 } else if ((actionFlags & kPMActionsFlagHasDarkWakePowerState) &&
6626 ((_pendingCapability & kIOPMSystemCapabilityGraphics) == 0) &&
6627 (changeFlags & kIOPMSynchronize)) {
6628 enableClamp = true;
6629 } else if ((actionFlags & kPMActionsFlagIsGraphicsDriver) &&
6630 (_systemTransitionType == kSystemTransitionSleep)) {
6631 // For graphics drivers, clamp power when entering
6632 // system sleep. Not when dropping to dark wake.
6633 enableClamp = true;
6634 }
6635
6636 if (enableClamp) {
6637 actions->state |= kPMActionsStatePowerClamped;
6638 DLOG("power clamp enabled %s %qx, pendingCap 0x%x, ps %d, cflags 0x%x\n",
6639 service->getName(), service->getRegistryEntryID(),
6640 _pendingCapability, powerState, changeFlags);
6641 }
6642 } else if ((actions->state & kPMActionsStatePowerClamped) != 0) {
6643 bool disableClamp = false;
6644
6645 if ((actionFlags & (
6646 kPMActionsFlagIsDisplayWrangler |
6647 kPMActionsFlagIsGraphicsDriver)) &&
6648 (_pendingCapability & kIOPMSystemCapabilityGraphics)) {
6649 disableClamp = true;
6650 } else if ((actionFlags & kPMActionsFlagIsAudioDriver) &&
6651 (_pendingCapability & kIOPMSystemCapabilityAudio)) {
6652 disableClamp = true;
6653 } else if ((actionFlags & kPMActionsFlagHasDarkWakePowerState) &&
6654 (_pendingCapability & kIOPMSystemCapabilityGraphics)) {
6655 disableClamp = true;
6656 }
6657
6658 if (disableClamp) {
6659 actions->state &= ~kPMActionsStatePowerClamped;
6660 DLOG("power clamp removed %s %qx, pendingCap 0x%x, ps %d, cflags 0x%x\n",
6661 service->getName(), service->getRegistryEntryID(),
6662 _pendingCapability, powerState, changeFlags);
6663 }
6664 }
6665
6666 if (actions->state & kPMActionsStatePowerClamped) {
6667 uint32_t maxPowerState = 0;
6668
6669 // Determine the max power state allowed when clamp is enabled
6670 if (changeFlags & (kIOPMDomainDidChange | kIOPMDomainWillChange)) {
6671 // Parent intiated power state changes
6672 if ((service->getPowerState() > maxPowerState) &&
6673 (actionFlags & kPMActionsFlagIsDisplayWrangler)) {
6674 maxPowerState++;
6675
6676 // Remove lingering effects of any tickle before entering
6677 // dark wake. It will take a new tickle to return to full
6678 // wake, so the existing tickle state is useless.
6679
6680 if (changeFlags & kIOPMDomainDidChange) {
6681 *inOutChangeFlags |= kIOPMExpireIdleTimer;
6682 }
6683 } else if (actionFlags & kPMActionsFlagIsGraphicsDriver) {
6684 maxPowerState++;
6685 } else if (actionFlags & kPMActionsFlagHasDarkWakePowerState) {
6686 maxPowerState = actions->darkWakePowerState;
6687 }
6688 } else {
6689 // Deny all self-initiated changes when power is limited.
6690 // Wrangler tickle should never defeat the limiter.
6691 maxPowerState = service->getPowerState();
6692 }
6693
6694 if (powerState > maxPowerState) {
6695 DLOG("power clamped %s %qx, ps %u->%u, cflags 0x%x)\n",
6696 service->getName(), service->getRegistryEntryID(),
6697 powerState, maxPowerState, changeFlags);
6698 *inOutPowerState = maxPowerState;
6699
6700 if (darkWakePostTickle &&
6701 (actionFlags & kPMActionsFlagIsDisplayWrangler) &&
6702 (changeFlags & kIOPMDomainWillChange) &&
6703 ((gDarkWakeFlags & kDarkWakeFlagPromotionMask) ==
6704 kDarkWakeFlagPromotionEarly)) {
6705 darkWakePostTickle = false;
6706 reportUserInput();
6707 }
6708 }
6709
6710 if (!darkWakePowerClamped && (changeFlags & kIOPMDomainDidChange)) {
6711 if (darkWakeLogClamp) {
6712 AbsoluteTime now;
6713 uint64_t nsec;
6714
6715 clock_get_uptime(&now);
6716 SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
6717 absolutetime_to_nanoseconds(now, &nsec);
6718 DLOG("dark wake power clamped after %u ms\n",
6719 ((int)((nsec) / NSEC_PER_MSEC)));
6720 }
6721 darkWakePowerClamped = true;
6722 }
6723 }
6724 }
6725
6726 void
handleActivityTickleForDisplayWrangler(IOService * service,IOPMActions * actions)6727 IOPMrootDomain::handleActivityTickleForDisplayWrangler(
6728 IOService * service,
6729 IOPMActions * actions )
6730 {
6731 #if DISPLAY_WRANGLER_PRESENT
6732 // Warning: Not running in PM work loop context - don't modify state !!!
6733 // Trap tickle directed to IODisplayWrangler while running with graphics
6734 // capability suppressed.
6735
6736 assert(service == wrangler);
6737
6738 clock_get_uptime(&userActivityTime);
6739 bool aborting = ((lastSleepReason == kIOPMSleepReasonIdle)
6740 || (lastSleepReason == kIOPMSleepReasonMaintenance)
6741 || (lastSleepReason == kIOPMSleepReasonSoftware));
6742 if (aborting) {
6743 userActivityCount++;
6744 DLOG("display wrangler tickled1 %d lastSleepReason %d\n",
6745 userActivityCount, lastSleepReason);
6746 }
6747
6748 if (!darkWakeExit && ((_pendingCapability & kIOPMSystemCapabilityGraphics) == 0)) {
6749 DLOG("display wrangler tickled\n");
6750 if (kIOLogPMRootDomain & gIOKitDebug) {
6751 OSReportWithBacktrace("Dark wake display tickle");
6752 }
6753 if (pmPowerStateQueue) {
6754 pmPowerStateQueue->submitPowerEvent(
6755 kPowerEventPolicyStimulus,
6756 (void *) kStimulusDarkWakeActivityTickle,
6757 true /* set wake type */ );
6758 }
6759 }
6760 #endif /* DISPLAY_WRANGLER_PRESENT */
6761 }
6762
6763 void
handleUpdatePowerClientForDisplayWrangler(IOService * service,IOPMActions * actions,const OSSymbol * powerClient,IOPMPowerStateIndex oldPowerState,IOPMPowerStateIndex newPowerState)6764 IOPMrootDomain::handleUpdatePowerClientForDisplayWrangler(
6765 IOService * service,
6766 IOPMActions * actions,
6767 const OSSymbol * powerClient,
6768 IOPMPowerStateIndex oldPowerState,
6769 IOPMPowerStateIndex newPowerState )
6770 {
6771 #if DISPLAY_WRANGLER_PRESENT
6772 assert(service == wrangler);
6773
6774 // This function implements half of the user active detection
6775 // by monitoring changes to the display wrangler's device desire.
6776 //
6777 // User becomes active when either:
6778 // 1. Wrangler's DeviceDesire increases to max, but wrangler is already
6779 // in max power state. This desire change in absence of a power state
6780 // change is detected within. This handles the case when user becomes
6781 // active while the display is already lit by setDisplayPowerOn().
6782 //
6783 // 2. Power state change to max, and DeviceDesire is also at max.
6784 // Handled by displayWranglerNotification().
6785 //
6786 // User becomes inactive when DeviceDesire drops to sleep state or below.
6787
6788 DLOG("wrangler %s (ps %u, %u->%u)\n",
6789 powerClient->getCStringNoCopy(),
6790 (uint32_t) service->getPowerState(),
6791 (uint32_t) oldPowerState, (uint32_t) newPowerState);
6792
6793 if (powerClient == gIOPMPowerClientDevice) {
6794 if ((newPowerState > oldPowerState) &&
6795 (newPowerState == kWranglerPowerStateMax) &&
6796 (service->getPowerState() == kWranglerPowerStateMax)) {
6797 evaluatePolicy( kStimulusEnterUserActiveState );
6798 } else if ((newPowerState < oldPowerState) &&
6799 (newPowerState <= kWranglerPowerStateSleep)) {
6800 evaluatePolicy( kStimulusLeaveUserActiveState );
6801 }
6802 }
6803
6804 if (newPowerState <= kWranglerPowerStateSleep) {
6805 evaluatePolicy( kStimulusDisplayWranglerSleep );
6806 } else if (newPowerState == kWranglerPowerStateMax) {
6807 evaluatePolicy( kStimulusDisplayWranglerWake );
6808 }
6809 #endif /* DISPLAY_WRANGLER_PRESENT */
6810 }
6811
6812 //******************************************************************************
6813 // User active state management
6814 //******************************************************************************
6815
6816 void
preventTransitionToUserActive(bool prevent)6817 IOPMrootDomain::preventTransitionToUserActive( bool prevent )
6818 {
6819 #if DISPLAY_WRANGLER_PRESENT
6820 _preventUserActive = prevent;
6821 if (wrangler && !_preventUserActive) {
6822 // Allowing transition to user active, but the wrangler may have
6823 // already powered ON in case of sleep cancel/revert. Poll the
6824 // same conditions checked for in displayWranglerNotification()
6825 // to bring the user active state up to date.
6826
6827 if ((wrangler->getPowerState() == kWranglerPowerStateMax) &&
6828 (wrangler->getPowerStateForClient(gIOPMPowerClientDevice) ==
6829 kWranglerPowerStateMax)) {
6830 evaluatePolicy( kStimulusEnterUserActiveState );
6831 }
6832 }
6833 #endif /* DISPLAY_WRANGLER_PRESENT */
6834 }
6835
6836 //******************************************************************************
6837 // Approve usage of delayed child notification by PM.
6838 //******************************************************************************
6839
6840 bool
shouldDelayChildNotification(IOService * service)6841 IOPMrootDomain::shouldDelayChildNotification(
6842 IOService * service )
6843 {
6844 if ((kFullWakeReasonNone == fullWakeReason) &&
6845 (kSystemTransitionWake == _systemTransitionType)) {
6846 DLOG("%s: delay child notify\n", service->getName());
6847 return true;
6848 }
6849 return false;
6850 }
6851
6852 //******************************************************************************
6853 // PM actions for PCI device.
6854 //******************************************************************************
6855
6856 void
handlePowerChangeStartForPCIDevice(IOService * service,IOPMActions * actions,const IOPMRequest * request,IOPMPowerStateIndex powerState,IOPMPowerChangeFlags * inOutChangeFlags)6857 IOPMrootDomain::handlePowerChangeStartForPCIDevice(
6858 IOService * service,
6859 IOPMActions * actions,
6860 const IOPMRequest * request,
6861 IOPMPowerStateIndex powerState,
6862 IOPMPowerChangeFlags * inOutChangeFlags )
6863 {
6864 pmTracer->tracePCIPowerChange(
6865 PMTraceWorker::kPowerChangeStart,
6866 service, *inOutChangeFlags,
6867 (actions->flags & kPMActionsPCIBitNumberMask));
6868 }
6869
6870 void
handlePowerChangeDoneForPCIDevice(IOService * service,IOPMActions * actions,const IOPMRequest * request,IOPMPowerStateIndex powerState,IOPMPowerChangeFlags changeFlags)6871 IOPMrootDomain::handlePowerChangeDoneForPCIDevice(
6872 IOService * service,
6873 IOPMActions * actions,
6874 const IOPMRequest * request,
6875 IOPMPowerStateIndex powerState,
6876 IOPMPowerChangeFlags changeFlags )
6877 {
6878 pmTracer->tracePCIPowerChange(
6879 PMTraceWorker::kPowerChangeCompleted,
6880 service, changeFlags,
6881 (actions->flags & kPMActionsPCIBitNumberMask));
6882 }
6883
6884 //******************************************************************************
6885 // registerInterest
6886 //
6887 // Override IOService::registerInterest() for root domain clients.
6888 //******************************************************************************
6889
6890 class IOPMServiceInterestNotifier : public _IOServiceInterestNotifier
6891 {
6892 friend class IOPMrootDomain;
6893 OSDeclareDefaultStructors(IOPMServiceInterestNotifier);
6894
6895 protected:
6896 uint32_t ackTimeoutCnt;
6897 uint32_t msgType; // Last type seen by the message filter
6898 uint32_t lastSleepWakeMsgType;
6899 uint32_t msgIndex;
6900 uint32_t maxMsgDelayMS;
6901 uint32_t maxAckDelayMS;
6902 uint64_t msgAbsTime;
6903 uint64_t uuid0;
6904 uint64_t uuid1;
6905 OSSharedPtr<const OSSymbol> identifier;
6906 OSSharedPtr<const OSSymbol> clientName;
6907 };
6908
OSDefineMetaClassAndStructors(IOPMServiceInterestNotifier,_IOServiceInterestNotifier)6909 OSDefineMetaClassAndStructors(IOPMServiceInterestNotifier, _IOServiceInterestNotifier)
6910
6911 OSSharedPtr<IONotifier>
6912 IOPMrootDomain::registerInterest(
6913 const OSSymbol * typeOfInterest,
6914 IOServiceInterestHandler handler,
6915 void * target, void * ref )
6916 {
6917 IOPMServiceInterestNotifier* notifier;
6918 bool isSystemCapabilityClient;
6919 bool isKernelCapabilityClient;
6920 IOReturn rc = kIOReturnError;
6921
6922 isSystemCapabilityClient = typeOfInterest &&
6923 typeOfInterest->isEqualTo(kIOPMSystemCapabilityInterest);
6924
6925 isKernelCapabilityClient = typeOfInterest &&
6926 typeOfInterest->isEqualTo(gIOPriorityPowerStateInterest);
6927
6928 if (isSystemCapabilityClient) {
6929 typeOfInterest = gIOAppPowerStateInterest;
6930 }
6931
6932 notifier = new IOPMServiceInterestNotifier;
6933 if (!notifier) {
6934 return NULL;
6935 }
6936
6937 if (notifier->init()) {
6938 rc = super::registerInterestForNotifier(notifier, typeOfInterest, handler, target, ref);
6939 }
6940 if (rc != kIOReturnSuccess) {
6941 OSSafeReleaseNULL(notifier);
6942 return NULL;
6943 }
6944
6945 notifier->ackTimeoutCnt = 0;
6946
6947 if (pmPowerStateQueue) {
6948 if (isSystemCapabilityClient) {
6949 notifier->retain();
6950 if (pmPowerStateQueue->submitPowerEvent(
6951 kPowerEventRegisterSystemCapabilityClient, notifier) == false) {
6952 notifier->release();
6953 }
6954 }
6955
6956 if (isKernelCapabilityClient) {
6957 notifier->retain();
6958 if (pmPowerStateQueue->submitPowerEvent(
6959 kPowerEventRegisterKernelCapabilityClient, notifier) == false) {
6960 notifier->release();
6961 }
6962 }
6963 }
6964
6965 OSSharedPtr<OSData> data;
6966 uint8_t *uuid = NULL;
6967 OSSharedPtr<OSKext> kext = OSKext::lookupKextWithAddress((vm_address_t)handler);
6968 if (kext) {
6969 data = kext->copyUUID();
6970 }
6971 if (data && (data->getLength() == sizeof(uuid_t))) {
6972 uuid = (uint8_t *)(data->getBytesNoCopy());
6973
6974 notifier->uuid0 = ((uint64_t)(uuid[0]) << 56) | ((uint64_t)(uuid[1]) << 48) | ((uint64_t)(uuid[2]) << 40) |
6975 ((uint64_t)(uuid[3]) << 32) | ((uint64_t)(uuid[4]) << 24) | ((uint64_t)(uuid[5]) << 16) |
6976 ((uint64_t)(uuid[6]) << 8) | (uuid[7]);
6977 notifier->uuid1 = ((uint64_t)(uuid[8]) << 56) | ((uint64_t)(uuid[9]) << 48) | ((uint64_t)(uuid[10]) << 40) |
6978 ((uint64_t)(uuid[11]) << 32) | ((uint64_t)(uuid[12]) << 24) | ((uint64_t)(uuid[13]) << 16) |
6979 ((uint64_t)(uuid[14]) << 8) | (uuid[15]);
6980
6981 notifier->identifier = copyKextIdentifierWithAddress((vm_address_t) handler);
6982 }
6983 return OSSharedPtr<IOPMServiceInterestNotifier>(notifier, OSNoRetain);
6984 }
6985
6986 //******************************************************************************
6987 // systemMessageFilter
6988 //
6989 //******************************************************************************
6990
6991 bool
systemMessageFilter(void * object,void * arg1,void * arg2,void * arg3)6992 IOPMrootDomain::systemMessageFilter(
6993 void * object, void * arg1, void * arg2, void * arg3 )
6994 {
6995 const IOPMInterestContext * context = (const IOPMInterestContext *) arg1;
6996 bool isCapMsg = (context->messageType == kIOMessageSystemCapabilityChange);
6997 bool isCapPowerd = (object == (void *) systemCapabilityNotifier.get());
6998 bool isCapClient = false;
6999 bool allow = false;
7000 OSBoolean **waitForReply = (typeof(waitForReply))arg3;
7001 IOPMServiceInterestNotifier *notifier;
7002
7003 notifier = OSDynamicCast(IOPMServiceInterestNotifier, (OSObject *)object);
7004
7005 do {
7006 // When powerd and kernel priority clients register capability interest,
7007 // the power tree is sync'ed to inform those clients about the current
7008 // system capability. Only allow capability change messages during sync.
7009 if ((kSystemTransitionNewCapClient == _systemTransitionType) &&
7010 (!isCapMsg || !_joinedCapabilityClients ||
7011 !_joinedCapabilityClients->containsObject((OSObject *) object))) {
7012 break;
7013 }
7014
7015 // Capability change message for powerd and kernel clients
7016 if (isCapMsg) {
7017 // Kernel priority clients
7018 if ((context->notifyType == kNotifyPriority) ||
7019 (context->notifyType == kNotifyCapabilityChangePriority)) {
7020 isCapClient = true;
7021 }
7022
7023 // powerd will maintain two client registrations with root domain.
7024 // isCapPowerd will be TRUE for any message targeting the powerd
7025 // exclusive (capability change) interest registration.
7026 if (isCapPowerd && (context->notifyType == kNotifyCapabilityChangeApps)) {
7027 isCapClient = true;
7028 }
7029 }
7030
7031 if (isCapClient) {
7032 IOPMSystemCapabilityChangeParameters * capArgs =
7033 (IOPMSystemCapabilityChangeParameters *) arg2;
7034
7035 if (kSystemTransitionNewCapClient == _systemTransitionType) {
7036 capArgs->fromCapabilities = 0;
7037 capArgs->toCapabilities = _currentCapability;
7038 capArgs->changeFlags = 0;
7039 } else {
7040 capArgs->fromCapabilities = _currentCapability;
7041 capArgs->toCapabilities = _pendingCapability;
7042
7043 if (context->isPreChange) {
7044 capArgs->changeFlags = kIOPMSystemCapabilityWillChange;
7045 } else {
7046 capArgs->changeFlags = kIOPMSystemCapabilityDidChange;
7047 }
7048
7049 if (isCapPowerd && context->isPreChange) {
7050 toldPowerdCapWillChange = true;
7051 }
7052 }
7053
7054 // App level capability change messages must only go to powerd.
7055 // Wait for response post-change if capabilitiy is increasing.
7056 // Wait for response pre-change if capability is decreasing.
7057
7058 if ((context->notifyType == kNotifyCapabilityChangeApps) && waitForReply &&
7059 ((capabilityLoss && context->isPreChange) ||
7060 (!capabilityLoss && !context->isPreChange))) {
7061 *waitForReply = kOSBooleanTrue;
7062 }
7063
7064 allow = true;
7065 break;
7066 }
7067
7068 // powerd will always receive CanSystemSleep, even for a demand sleep.
7069 // It will also have a final chance to veto sleep after all clients
7070 // have responded to SystemWillSleep
7071
7072 if ((kIOMessageCanSystemSleep == context->messageType) ||
7073 (kIOMessageSystemWillNotSleep == context->messageType)) {
7074 if (isCapPowerd) {
7075 allow = true;
7076 break;
7077 }
7078
7079 // Demand sleep, don't ask apps for permission
7080 if (context->changeFlags & kIOPMSkipAskPowerDown) {
7081 break;
7082 }
7083 }
7084
7085 if (kIOPMMessageLastCallBeforeSleep == context->messageType) {
7086 if (isCapPowerd && CAP_HIGHEST(kIOPMSystemCapabilityGraphics) &&
7087 (fullToDarkReason == kIOPMSleepReasonIdle)) {
7088 allow = true;
7089 }
7090 break;
7091 }
7092
7093 // Drop capability change messages for legacy clients.
7094 // Drop legacy system sleep messages for powerd capability interest.
7095 if (isCapMsg || isCapPowerd) {
7096 break;
7097 }
7098
7099 // Not a capability change message.
7100 // Perform message filtering based on _systemMessageClientMask.
7101
7102 if ((context->notifyType == kNotifyApps) &&
7103 (_systemMessageClientMask & kSystemMessageClientLegacyApp)) {
7104 if (!notifier) {
7105 break;
7106 }
7107
7108 if ((notifier->lastSleepWakeMsgType == context->messageType) &&
7109 (notifier->lastSleepWakeMsgType == kIOMessageSystemWillPowerOn)) {
7110 break; // drop any duplicate WillPowerOn for AOT devices
7111 }
7112
7113 allow = true;
7114
7115 if (waitForReply) {
7116 if (notifier->ackTimeoutCnt >= 3) {
7117 *waitForReply = kOSBooleanFalse;
7118 } else {
7119 *waitForReply = kOSBooleanTrue;
7120 }
7121 }
7122 } else if ((context->notifyType == kNotifyPriority) &&
7123 (_systemMessageClientMask & kSystemMessageClientKernel)) {
7124 allow = true;
7125 }
7126
7127 // Check sleep/wake message ordering
7128 if (allow) {
7129 if (context->messageType == kIOMessageSystemWillSleep ||
7130 context->messageType == kIOMessageSystemWillPowerOn ||
7131 context->messageType == kIOMessageSystemHasPoweredOn) {
7132 notifier->lastSleepWakeMsgType = context->messageType;
7133 }
7134 }
7135 } while (false);
7136
7137 if (allow && isCapMsg && _joinedCapabilityClients) {
7138 _joinedCapabilityClients->removeObject((OSObject *) object);
7139 if (_joinedCapabilityClients->getCount() == 0) {
7140 DMSG("destroyed capability client set %p\n",
7141 OBFUSCATE(_joinedCapabilityClients.get()));
7142 _joinedCapabilityClients.reset();
7143 }
7144 }
7145 if (notifier) {
7146 // Record the last seen message type even if the message is dropped
7147 // for traceFilteredNotification().
7148 notifier->msgType = context->messageType;
7149 }
7150
7151 return allow;
7152 }
7153
7154 //******************************************************************************
7155 // setMaintenanceWakeCalendar
7156 //
7157 //******************************************************************************
7158
7159 IOReturn
setMaintenanceWakeCalendar(const IOPMCalendarStruct * calendar)7160 IOPMrootDomain::setMaintenanceWakeCalendar(
7161 const IOPMCalendarStruct * calendar )
7162 {
7163 OSSharedPtr<OSData> data;
7164 IOReturn ret = 0;
7165
7166 if (!calendar) {
7167 return kIOReturnBadArgument;
7168 }
7169
7170 data = OSData::withValue(*calendar);
7171 if (!data) {
7172 return kIOReturnNoMemory;
7173 }
7174
7175 if (kPMCalendarTypeMaintenance == calendar->selector) {
7176 ret = setPMSetting(gIOPMSettingMaintenanceWakeCalendarKey.get(), data.get());
7177 } else if (kPMCalendarTypeSleepService == calendar->selector) {
7178 ret = setPMSetting(gIOPMSettingSleepServiceWakeCalendarKey.get(), data.get());
7179 }
7180
7181 return ret;
7182 }
7183
7184 // MARK: -
7185 // MARK: Display Wrangler
7186
7187 //******************************************************************************
7188 // displayWranglerNotification
7189 //
7190 // Handle the notification when the IODisplayWrangler changes power state.
7191 //******************************************************************************
7192
7193 IOReturn
displayWranglerNotification(void * target,void * refCon,UInt32 messageType,IOService * service,void * messageArgument,vm_size_t argSize)7194 IOPMrootDomain::displayWranglerNotification(
7195 void * target, void * refCon,
7196 UInt32 messageType, IOService * service,
7197 void * messageArgument, vm_size_t argSize )
7198 {
7199 #if DISPLAY_WRANGLER_PRESENT
7200 IOPMPowerStateIndex displayPowerState;
7201 IOPowerStateChangeNotification * params =
7202 (IOPowerStateChangeNotification *) messageArgument;
7203
7204 if ((messageType != kIOMessageDeviceWillPowerOff) &&
7205 (messageType != kIOMessageDeviceHasPoweredOn)) {
7206 return kIOReturnUnsupported;
7207 }
7208
7209 ASSERT_GATED();
7210 if (!gRootDomain) {
7211 return kIOReturnUnsupported;
7212 }
7213
7214 displayPowerState = params->stateNumber;
7215 DLOG("wrangler %s ps %d\n",
7216 getIOMessageString(messageType), (uint32_t) displayPowerState);
7217
7218 switch (messageType) {
7219 case kIOMessageDeviceWillPowerOff:
7220 // Display wrangler has dropped power due to display idle
7221 // or force system sleep.
7222 //
7223 // 4 Display ON kWranglerPowerStateMax
7224 // 3 Display Dim kWranglerPowerStateDim
7225 // 2 Display Sleep kWranglerPowerStateSleep
7226 // 1 Not visible to user
7227 // 0 Not visible to user kWranglerPowerStateMin
7228
7229 if (displayPowerState <= kWranglerPowerStateSleep) {
7230 gRootDomain->evaluatePolicy( kStimulusDisplayWranglerSleep );
7231 }
7232 break;
7233
7234 case kIOMessageDeviceHasPoweredOn:
7235 // Display wrangler has powered on due to user activity
7236 // or wake from sleep.
7237
7238 if (kWranglerPowerStateMax == displayPowerState) {
7239 gRootDomain->evaluatePolicy( kStimulusDisplayWranglerWake );
7240
7241 // See comment in handleUpdatePowerClientForDisplayWrangler
7242 if (service->getPowerStateForClient(gIOPMPowerClientDevice) ==
7243 kWranglerPowerStateMax) {
7244 gRootDomain->evaluatePolicy( kStimulusEnterUserActiveState );
7245 }
7246 }
7247 break;
7248 }
7249 #endif /* DISPLAY_WRANGLER_PRESENT */
7250 return kIOReturnUnsupported;
7251 }
7252
7253 //******************************************************************************
7254 // reportUserInput
7255 //
7256 //******************************************************************************
7257
7258 void
updateUserActivity(void)7259 IOPMrootDomain::updateUserActivity( void )
7260 {
7261 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
7262 clock_get_uptime(&userActivityTime);
7263 bool aborting = ((lastSleepReason == kIOPMSleepReasonSoftware)
7264 || (lastSleepReason == kIOPMSleepReasonIdle)
7265 || (lastSleepReason == kIOPMSleepReasonMaintenance));
7266 if (aborting) {
7267 userActivityCount++;
7268 DLOG("user activity reported %d lastSleepReason %d\n", userActivityCount, lastSleepReason);
7269 }
7270 #endif
7271 }
7272 void
reportUserInput(void)7273 IOPMrootDomain::reportUserInput( void )
7274 {
7275 if (wrangler) {
7276 wrangler->activityTickle(0, 0);
7277 }
7278 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
7279 // Update user activity
7280 updateUserActivity();
7281
7282 if (!darkWakeExit && ((_pendingCapability & kIOPMSystemCapabilityGraphics) == 0)) {
7283 // update user active abs time
7284 clock_get_uptime(&gUserActiveAbsTime);
7285 pmPowerStateQueue->submitPowerEvent(
7286 kPowerEventPolicyStimulus,
7287 (void *) kStimulusDarkWakeActivityTickle,
7288 true /* set wake type */ );
7289 }
7290 #endif
7291 }
7292
7293 void
requestUserActive(IOService * device,const char * reason)7294 IOPMrootDomain::requestUserActive(IOService *device, const char *reason)
7295 {
7296 #if DISPLAY_WRANGLER_PRESENT
7297 if (wrangler) {
7298 wrangler->activityTickle(0, 0);
7299 }
7300 #else
7301 if (!device) {
7302 DLOG("requestUserActive: device is null\n");
7303 return;
7304 }
7305 OSSharedPtr<const OSSymbol> deviceName = device->copyName();
7306 uint64_t registryID = device->getRegistryEntryID();
7307
7308 if (!deviceName || !registryID) {
7309 DLOG("requestUserActive: no device name or registry entry\n");
7310 return;
7311 }
7312 const char *name = deviceName->getCStringNoCopy();
7313 char payload[128];
7314 snprintf(payload, sizeof(payload), "%s:%s", name, reason);
7315 DLOG("requestUserActive from %s (0x%llx) for %s\n", name, registryID, reason);
7316 messageClient(kIOPMMessageRequestUserActive, systemCapabilityNotifier.get(), (void *)payload, sizeof(payload));
7317 #endif
7318 }
7319
7320 //******************************************************************************
7321 // latchDisplayWranglerTickle
7322 //******************************************************************************
7323
7324 bool
latchDisplayWranglerTickle(bool latch)7325 IOPMrootDomain::latchDisplayWranglerTickle( bool latch )
7326 {
7327 #if DISPLAY_WRANGLER_PRESENT
7328 if (latch) {
7329 if (!(_currentCapability & kIOPMSystemCapabilityGraphics) &&
7330 !(_pendingCapability & kIOPMSystemCapabilityGraphics) &&
7331 !checkSystemCanSustainFullWake()) {
7332 // Currently in dark wake, and not transitioning to full wake.
7333 // Full wake is unsustainable, so latch the tickle to prevent
7334 // the display from lighting up momentarily.
7335 wranglerTickled = true;
7336 } else {
7337 wranglerTickled = false;
7338 }
7339 } else if (wranglerTickled && checkSystemCanSustainFullWake()) {
7340 wranglerTickled = false;
7341
7342 pmPowerStateQueue->submitPowerEvent(
7343 kPowerEventPolicyStimulus,
7344 (void *) kStimulusDarkWakeActivityTickle );
7345 }
7346
7347 return wranglerTickled;
7348 #else /* ! DISPLAY_WRANGLER_PRESENT */
7349 return false;
7350 #endif /* ! DISPLAY_WRANGLER_PRESENT */
7351 }
7352
7353 //******************************************************************************
7354 // setDisplayPowerOn
7355 //
7356 // For root domain user client
7357 //******************************************************************************
7358
7359 void
setDisplayPowerOn(uint32_t options)7360 IOPMrootDomain::setDisplayPowerOn( uint32_t options )
7361 {
7362 pmPowerStateQueue->submitPowerEvent( kPowerEventSetDisplayPowerOn,
7363 (void *) NULL, options );
7364 }
7365
7366 // MARK: -
7367 // MARK: System PM Policy
7368
7369 //******************************************************************************
7370 // checkSystemSleepAllowed
7371 //
7372 //******************************************************************************
7373
7374 bool
checkSystemSleepAllowed(IOOptionBits options,uint32_t sleepReason)7375 IOPMrootDomain::checkSystemSleepAllowed( IOOptionBits options,
7376 uint32_t sleepReason )
7377 {
7378 uint32_t err = 0;
7379
7380 // Conditions that prevent idle and demand system sleep.
7381
7382 do {
7383 if (gSleepDisabledFlag) {
7384 err = kPMConfigPreventSystemSleep;
7385 break;
7386 }
7387
7388 if (userDisabledAllSleep) {
7389 err = kPMUserDisabledAllSleep; // 1. user-space sleep kill switch
7390 break;
7391 }
7392
7393 if (systemBooting || systemShutdown || gWillShutdown) {
7394 err = kPMSystemRestartBootingInProgress; // 2. restart or shutdown in progress
7395 break;
7396 }
7397
7398 if (options == 0) {
7399 break;
7400 }
7401
7402 // Conditions above pegs the system at full wake.
7403 // Conditions below prevent system sleep but does not prevent
7404 // dark wake, and must be called from gated context.
7405
7406 #if !CONFIG_SLEEP
7407 err = kPMConfigPreventSystemSleep; // 3. config does not support sleep
7408 break;
7409 #endif
7410
7411 if (lowBatteryCondition || thermalWarningState || thermalEmergencyState) {
7412 break; // always sleep on low battery or when in thermal warning/emergency state
7413 }
7414
7415 if (sleepReason == kIOPMSleepReasonDarkWakeThermalEmergency) {
7416 break; // always sleep on dark wake thermal emergencies
7417 }
7418
7419 if (preventSystemSleepList->getCount() != 0) {
7420 err = kPMChildPreventSystemSleep; // 4. child prevent system sleep clamp
7421 break;
7422 }
7423
7424 if (_driverKitMatchingAssertionCount != 0) {
7425 err = kPMCPUAssertion;
7426 break;
7427 }
7428
7429 if (getPMAssertionLevel( kIOPMDriverAssertionCPUBit ) ==
7430 kIOPMDriverAssertionLevelOn) {
7431 err = kPMCPUAssertion; // 5. CPU assertion
7432 break;
7433 }
7434
7435 if (pciCantSleepValid) {
7436 if (pciCantSleepFlag) {
7437 err = kPMPCIUnsupported; // 6. PCI card does not support PM (cached)
7438 }
7439 break;
7440 } else if (sleepSupportedPEFunction &&
7441 CAP_HIGHEST(kIOPMSystemCapabilityGraphics)) {
7442 IOReturn ret;
7443 OSBitAndAtomic(~kPCICantSleep, &platformSleepSupport);
7444 ret = getPlatform()->callPlatformFunction(
7445 sleepSupportedPEFunction.get(), false,
7446 NULL, NULL, NULL, NULL);
7447 pciCantSleepValid = true;
7448 pciCantSleepFlag = false;
7449 if ((platformSleepSupport & kPCICantSleep) ||
7450 ((ret != kIOReturnSuccess) && (ret != kIOReturnUnsupported))) {
7451 err = 6; // 6. PCI card does not support PM
7452 pciCantSleepFlag = true;
7453 break;
7454 }
7455 }
7456 }while (false);
7457
7458 if (err) {
7459 DLOG("System sleep prevented by %s\n", getSystemSleepPreventerString(err));
7460 return false;
7461 }
7462 return true;
7463 }
7464
7465 bool
checkSystemSleepEnabled(void)7466 IOPMrootDomain::checkSystemSleepEnabled( void )
7467 {
7468 return checkSystemSleepAllowed(0, 0);
7469 }
7470
7471 bool
checkSystemCanSleep(uint32_t sleepReason)7472 IOPMrootDomain::checkSystemCanSleep( uint32_t sleepReason )
7473 {
7474 ASSERT_GATED();
7475 return checkSystemSleepAllowed(1, sleepReason);
7476 }
7477
7478 //******************************************************************************
7479 // checkSystemCanSustainFullWake
7480 //******************************************************************************
7481
7482 bool
checkSystemCanSustainFullWake(void)7483 IOPMrootDomain::checkSystemCanSustainFullWake( void )
7484 {
7485 if (lowBatteryCondition || thermalWarningState || thermalEmergencyState) {
7486 // Low battery wake, or received a low battery notification
7487 // while system is awake. This condition will persist until
7488 // the following wake.
7489 return false;
7490 }
7491
7492 if (clamshellExists && clamshellClosed && !clamshellSleepDisableMask) {
7493 // Graphics state is unknown and external display might not be probed.
7494 // Do not incorporate state that requires graphics to be in max power
7495 // such as desktopMode or clamshellDisabled.
7496
7497 if (!acAdaptorConnected) {
7498 DLOG("full wake check: no AC\n");
7499 return false;
7500 }
7501 }
7502 return true;
7503 }
7504
7505 //******************************************************************************
7506 // mustHibernate
7507 //******************************************************************************
7508
7509 #if HIBERNATION
7510
7511 bool
mustHibernate(void)7512 IOPMrootDomain::mustHibernate( void )
7513 {
7514 return lowBatteryCondition || thermalWarningState;
7515 }
7516
7517 #endif /* HIBERNATION */
7518
7519 //******************************************************************************
7520 // AOT
7521 //******************************************************************************
7522
7523 // Tables for accumulated days in year by month, latter used for leap years
7524
7525 static const unsigned int daysbymonth[] =
7526 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
7527
7528 static const unsigned int lydaysbymonth[] =
7529 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 };
7530
7531 static int __unused
IOPMConvertSecondsToCalendar(clock_sec_t secs,IOPMCalendarStruct * dt)7532 IOPMConvertSecondsToCalendar(clock_sec_t secs, IOPMCalendarStruct * dt)
7533 {
7534 const unsigned int * dbm = daysbymonth;
7535 clock_sec_t n, x, y, z;
7536
7537 // Calculate seconds, minutes and hours
7538
7539 n = secs % (24 * 3600);
7540 dt->second = n % 60;
7541 n /= 60;
7542 dt->minute = n % 60;
7543 dt->hour = (typeof(dt->hour))(n / 60);
7544
7545 // Calculate day of week
7546
7547 n = secs / (24 * 3600);
7548 // dt->dayWeek = (n + 4) % 7;
7549
7550 // Calculate year
7551 // Rebase from days since Unix epoch (1/1/1970) store in 'n',
7552 // to days since 1/1/1968 to start on 4 year cycle, beginning
7553 // on a leap year.
7554
7555 n += (366 + 365);
7556
7557 // Every 4 year cycle will be exactly (366 + 365 * 3) = 1461 days.
7558 // Valid before 2100, since 2100 is not a leap year.
7559
7560 x = n / 1461; // number of 4 year cycles
7561 y = n % 1461; // days into current 4 year cycle
7562 z = 1968 + (4 * x);
7563
7564 // Add in years in the current 4 year cycle
7565
7566 if (y >= 366) {
7567 y -= 366; // days after the leap year
7568 n = y % 365; // days into the current year
7569 z += (1 + y / 365); // years after the past 4-yr cycle
7570 } else {
7571 n = y;
7572 dbm = lydaysbymonth;
7573 }
7574 if (z > 2099) {
7575 return 0;
7576 }
7577
7578 dt->year = (typeof(dt->year))z;
7579
7580 // Adjust remaining days value to start at 1
7581
7582 n += 1;
7583
7584 // Calculate month
7585
7586 for (x = 1; (n > dbm[x]) && (x < 12); x++) {
7587 continue;
7588 }
7589 dt->month = (typeof(dt->month))x;
7590
7591 // Calculate day of month
7592
7593 dt->day = (typeof(dt->day))(n - dbm[x - 1]);
7594
7595 return 1;
7596 }
7597
7598 static clock_sec_t
IOPMConvertCalendarToSeconds(const IOPMCalendarStruct * dt)7599 IOPMConvertCalendarToSeconds(const IOPMCalendarStruct * dt)
7600 {
7601 const unsigned int * dbm = daysbymonth;
7602 long y, secs, days;
7603
7604 if (dt->year < 1970 || dt->month > 12) {
7605 return 0;
7606 }
7607
7608 // Seconds elapsed in the current day
7609
7610 secs = dt->second + 60 * dt->minute + 3600 * dt->hour;
7611
7612 // Number of days from 1/1/70 to beginning of current year
7613 // Account for extra day every 4 years starting at 1973
7614
7615 y = dt->year - 1970;
7616 days = (y * 365) + ((y + 1) / 4);
7617
7618 // Change table if current year is a leap year
7619
7620 if ((dt->year % 4) == 0) {
7621 dbm = lydaysbymonth;
7622 }
7623
7624 // Add in days elapsed in the current year
7625
7626 days += (dt->day - 1) + dbm[dt->month - 1];
7627
7628 // Add accumulated days to accumulated seconds
7629
7630 secs += 24 * 3600 * days;
7631
7632 return secs;
7633 }
7634
7635 unsigned long
getRUN_STATE(void)7636 IOPMrootDomain::getRUN_STATE(void)
7637 {
7638 return (_aotNow && !(kIOPMWakeEventAOTExitFlags & _aotPendingFlags)) ? AOT_STATE : ON_STATE;
7639 }
7640
7641 bool
isAOTMode()7642 IOPMrootDomain::isAOTMode()
7643 {
7644 return _aotNow;
7645 }
7646
7647 IOReturn
setWakeTime(uint64_t wakeContinuousTime)7648 IOPMrootDomain::setWakeTime(uint64_t wakeContinuousTime)
7649 {
7650 clock_sec_t nowsecs, wakesecs;
7651 clock_usec_t nowmicrosecs, wakemicrosecs;
7652 uint64_t nowAbs, wakeAbs;
7653
7654 if (!_aotMode) {
7655 return kIOReturnNotReady;
7656 }
7657
7658 clock_gettimeofday_and_absolute_time(&nowsecs, &nowmicrosecs, &nowAbs);
7659 wakeAbs = continuoustime_to_absolutetime(wakeContinuousTime);
7660 if (wakeAbs < nowAbs) {
7661 printf(LOG_PREFIX "wakeAbs %qd < nowAbs %qd\n", wakeAbs, nowAbs);
7662 wakeAbs = nowAbs;
7663 }
7664 wakeAbs -= nowAbs;
7665 absolutetime_to_microtime(wakeAbs, &wakesecs, &wakemicrosecs);
7666
7667 wakesecs += nowsecs;
7668 wakemicrosecs += nowmicrosecs;
7669 if (wakemicrosecs >= USEC_PER_SEC) {
7670 wakesecs++;
7671 wakemicrosecs -= USEC_PER_SEC;
7672 }
7673 if (wakemicrosecs >= (USEC_PER_SEC / 10)) {
7674 wakesecs++;
7675 }
7676
7677 IOPMConvertSecondsToCalendar(wakesecs, &_aotWakeTimeCalendar);
7678
7679 if (_aotWakeTimeContinuous != wakeContinuousTime) {
7680 _aotWakeTimeContinuous = wakeContinuousTime;
7681 IOLog(LOG_PREFIX "setWakeTime: " YMDTF "\n", YMDT(&_aotWakeTimeCalendar));
7682 }
7683 _aotWakeTimeCalendar.selector = kPMCalendarTypeMaintenance;
7684 _aotWakeTimeUTC = wakesecs;
7685
7686 return kIOReturnSuccess;
7687 }
7688
7689 // assumes WAKEEVENT_LOCK
7690 bool
aotShouldExit(bool checkTimeSet,bool software)7691 IOPMrootDomain::aotShouldExit(bool checkTimeSet, bool software)
7692 {
7693 bool exitNow = false;
7694 const char * reason = "";
7695
7696 if (!_aotNow) {
7697 return false;
7698 }
7699
7700 if (software) {
7701 exitNow = true;
7702 _aotMetrics->softwareRequestCount++;
7703 reason = "software request";
7704 } else if (kIOPMWakeEventAOTExitFlags & _aotPendingFlags) {
7705 exitNow = true;
7706 reason = gWakeReasonString;
7707 } else if (checkTimeSet && (kPMCalendarTypeInvalid == _aotWakeTimeCalendar.selector)) {
7708 exitNow = true;
7709 _aotMetrics->noTimeSetCount++;
7710 reason = "flipbook expired";
7711 } else if ((kIOPMAOTModeRespectTimers & _aotMode) && _calendarWakeAlarmUTC) {
7712 clock_sec_t sec;
7713 clock_usec_t usec;
7714 clock_get_calendar_microtime(&sec, &usec);
7715 if (_calendarWakeAlarmUTC <= sec) {
7716 exitNow = true;
7717 _aotMetrics->rtcAlarmsCount++;
7718 reason = "user alarm";
7719 }
7720 }
7721 if (exitNow) {
7722 _aotPendingFlags |= kIOPMWakeEventAOTExit;
7723 IOLog(LOG_PREFIX "AOT exit for %s, sc %d po %d, cp %d, rj %d, ex %d, nt %d, rt %d\n",
7724 reason,
7725 _aotMetrics->sleepCount,
7726 _aotMetrics->possibleCount,
7727 _aotMetrics->confirmedPossibleCount,
7728 _aotMetrics->rejectedPossibleCount,
7729 _aotMetrics->expiredPossibleCount,
7730 _aotMetrics->noTimeSetCount,
7731 _aotMetrics->rtcAlarmsCount);
7732 }
7733 return exitNow;
7734 }
7735
7736 void
aotExit(bool cps)7737 IOPMrootDomain::aotExit(bool cps)
7738 {
7739 uint32_t savedMessageMask;
7740
7741 ASSERT_GATED();
7742 _aotNow = false;
7743 _aotReadyToFullWake = false;
7744 if (_aotTimerScheduled) {
7745 _aotTimerES->cancelTimeout();
7746 _aotTimerScheduled = false;
7747 }
7748 updateTasksSuspend(kTasksSuspendNoChange, kTasksSuspendUnsuspended);
7749
7750 _aotMetrics->totalTime += mach_absolute_time() - _aotLastWakeTime;
7751 _aotLastWakeTime = 0;
7752 if (_aotMetrics->sleepCount && (_aotMetrics->sleepCount <= kIOPMAOTMetricsKernelWakeCountMax)) {
7753 WAKEEVENT_LOCK();
7754 strlcpy(&_aotMetrics->kernelWakeReason[_aotMetrics->sleepCount - 1][0],
7755 gWakeReasonString,
7756 sizeof(_aotMetrics->kernelWakeReason[_aotMetrics->sleepCount]));
7757 WAKEEVENT_UNLOCK();
7758 }
7759
7760 _aotWakeTimeCalendar.selector = kPMCalendarTypeInvalid;
7761
7762 // Preserve the message mask since a system wake transition
7763 // may have already started and initialized the mask.
7764 savedMessageMask = _systemMessageClientMask;
7765 _systemMessageClientMask = kSystemMessageClientLegacyApp;
7766 tellClients(kIOMessageSystemWillPowerOn);
7767 _systemMessageClientMask = savedMessageMask | kSystemMessageClientLegacyApp;
7768
7769 if (cps) {
7770 changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonAOTExit);
7771 }
7772 }
7773
7774 void
aotEvaluate(IOTimerEventSource * timer)7775 IOPMrootDomain::aotEvaluate(IOTimerEventSource * timer)
7776 {
7777 bool exitNow;
7778
7779 IOLog("aotEvaluate(%d) 0x%x\n", (timer != NULL), _aotPendingFlags);
7780
7781 WAKEEVENT_LOCK();
7782 exitNow = aotShouldExit(false, false);
7783 if (timer != NULL) {
7784 _aotTimerScheduled = false;
7785 }
7786 WAKEEVENT_UNLOCK();
7787 if (exitNow) {
7788 aotExit(true);
7789 } else {
7790 #if 0
7791 if (_aotLingerTime) {
7792 uint64_t deadline;
7793 IOLog("aot linger before sleep\n");
7794 clock_absolutetime_interval_to_deadline(_aotLingerTime, &deadline);
7795 clock_delay_until(deadline);
7796 }
7797 #endif
7798 privateSleepSystem(kIOPMSleepReasonSoftware);
7799 }
7800 }
7801
7802 //******************************************************************************
7803 // adjustPowerState
7804 //
7805 // Conditions that affect our wake/sleep decision has changed.
7806 // If conditions dictate that the system must remain awake, clamp power
7807 // state to max with changePowerStateToPriv(ON). Otherwise if sleepASAP
7808 // is TRUE, then remove the power clamp and allow the power state to drop
7809 // to SLEEP_STATE.
7810 //******************************************************************************
7811
7812 void
adjustPowerState(bool sleepASAP)7813 IOPMrootDomain::adjustPowerState( bool sleepASAP )
7814 {
7815 DEBUG_LOG("adjustPowerState %s, asap %d, idleSleepEnabled %d\n",
7816 getPowerStateString((uint32_t) getPowerState()), sleepASAP, idleSleepEnabled);
7817
7818 ASSERT_GATED();
7819
7820 if (_aotNow) {
7821 bool exitNow;
7822
7823 if (AOT_STATE != getPowerState()) {
7824 return;
7825 }
7826 WAKEEVENT_LOCK();
7827 exitNow = aotShouldExit(true, false);
7828 if (!exitNow
7829 && !_aotTimerScheduled
7830 && (kIOPMWakeEventAOTPossibleExit == (kIOPMWakeEventAOTPossibleFlags & _aotPendingFlags))) {
7831 _aotTimerScheduled = true;
7832 if (_aotLingerTime) {
7833 _aotTimerES->setTimeout(_aotLingerTime);
7834 } else {
7835 _aotTimerES->setTimeout(800, kMillisecondScale);
7836 }
7837 }
7838 WAKEEVENT_UNLOCK();
7839 if (exitNow) {
7840 aotExit(true);
7841 } else {
7842 _aotReadyToFullWake = true;
7843 if (!_aotTimerScheduled) {
7844 if (kIOPMDriverAssertionLevelOn == getPMAssertionLevel(kIOPMDriverAssertionCPUBit)) {
7845 // Don't try to force sleep during AOT while IOMobileFramebuffer is holding a power assertion.
7846 // Doing so will result in the sleep being cancelled anyway,
7847 // but this check avoids unnecessary thrashing in the power state engine.
7848 return;
7849 }
7850 privateSleepSystem(kIOPMSleepReasonSoftware);
7851 }
7852 }
7853 return;
7854 }
7855
7856 if ((!idleSleepEnabled) || !checkSystemSleepEnabled()) {
7857 changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonAdjustPowerState);
7858 } else if (sleepASAP) {
7859 changePowerStateWithTagToPriv(SLEEP_STATE, kCPSReasonAdjustPowerState);
7860 }
7861 }
7862
7863 void
handleSetDisplayPowerOn(bool powerOn)7864 IOPMrootDomain::handleSetDisplayPowerOn(bool powerOn)
7865 {
7866 if (powerOn) {
7867 if (!checkSystemCanSustainFullWake()) {
7868 DLOG("System cannot sustain full wake\n");
7869 return;
7870 }
7871
7872 // Force wrangler to max power state. If system is in dark wake
7873 // this alone won't raise the wrangler's power state.
7874 if (wrangler) {
7875 wrangler->changePowerStateForRootDomain(kWranglerPowerStateMax);
7876 }
7877
7878 // System in dark wake, always requesting full wake should
7879 // not have any bad side-effects, even if the request fails.
7880
7881 if (!CAP_CURRENT(kIOPMSystemCapabilityGraphics)) {
7882 setProperty(kIOPMRootDomainWakeTypeKey, kIOPMRootDomainWakeTypeNotification);
7883 requestFullWake( kFullWakeReasonDisplayOn );
7884 }
7885 } else {
7886 // Relenquish desire to power up display.
7887 // Must first transition to state 1 since wrangler doesn't
7888 // power off the displays at state 0. At state 0 the root
7889 // domain is removed from the wrangler's power client list.
7890 if (wrangler) {
7891 wrangler->changePowerStateForRootDomain(kWranglerPowerStateMin + 1);
7892 wrangler->changePowerStateForRootDomain(kWranglerPowerStateMin);
7893 }
7894 }
7895 }
7896
7897 //******************************************************************************
7898 // dispatchPowerEvent
7899 //
7900 // IOPMPowerStateQueue callback function. Running on PM work loop thread.
7901 //******************************************************************************
7902
7903 void
dispatchPowerEvent(uint32_t event,void * arg0,uint64_t arg1)7904 IOPMrootDomain::dispatchPowerEvent(
7905 uint32_t event, void * arg0, uint64_t arg1 )
7906 {
7907 ASSERT_GATED();
7908
7909 switch (event) {
7910 case kPowerEventFeatureChanged:
7911 DMSG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7912 messageClients(kIOPMMessageFeatureChange, this);
7913 break;
7914
7915 case kPowerEventReceivedPowerNotification:
7916 DMSG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7917 handlePowerNotification((UInt32)(uintptr_t) arg0 );
7918 break;
7919
7920 case kPowerEventSystemBootCompleted:
7921 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7922 if (systemBooting) {
7923 systemBooting = false;
7924
7925 if (PE_get_default("sleep-disabled", &gSleepDisabledFlag, sizeof(gSleepDisabledFlag))) {
7926 DLOG("Setting gSleepDisabledFlag to %u from device tree\n", gSleepDisabledFlag);
7927 }
7928 if (lowBatteryCondition || thermalEmergencyState) {
7929 if (lowBatteryCondition) {
7930 privateSleepSystem(kIOPMSleepReasonLowPower);
7931 } else {
7932 privateSleepSystem(kIOPMSleepReasonThermalEmergency);
7933 }
7934 // The rest is unnecessary since the system is expected
7935 // to sleep immediately. The following wake will update
7936 // everything.
7937 break;
7938 }
7939
7940 sleepWakeDebugMemAlloc();
7941 saveFailureData2File();
7942
7943 // If lid is closed, re-send lid closed notification
7944 // now that booting is complete.
7945 if (clamshellClosed) {
7946 handlePowerNotification(kLocalEvalClamshellCommand);
7947 }
7948 evaluatePolicy( kStimulusAllowSystemSleepChanged );
7949 }
7950 break;
7951
7952 case kPowerEventSystemShutdown:
7953 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7954 if (kOSBooleanTrue == (OSBoolean *) arg0) {
7955 /* We set systemShutdown = true during shutdown
7956 * to prevent sleep at unexpected times while loginwindow is trying
7957 * to shutdown apps and while the OS is trying to transition to
7958 * complete power of.
7959 *
7960 * Set to true during shutdown, as soon as loginwindow shows
7961 * the "shutdown countdown dialog", through individual app
7962 * termination, and through black screen kernel shutdown.
7963 */
7964 systemShutdown = true;
7965 } else {
7966 /*
7967 * A shutdown was initiated, but then the shutdown
7968 * was cancelled, clearing systemShutdown to false here.
7969 */
7970 systemShutdown = false;
7971 }
7972 break;
7973
7974 case kPowerEventUserDisabledSleep:
7975 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7976 userDisabledAllSleep = (kOSBooleanTrue == (OSBoolean *) arg0);
7977 break;
7978
7979 case kPowerEventRegisterSystemCapabilityClient:
7980 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7981
7982 // reset() handles the arg0 == nullptr case for us
7983 systemCapabilityNotifier.reset((IONotifier *) arg0, OSRetain);
7984 /* intentional fall-through */
7985 [[clang::fallthrough]];
7986
7987 case kPowerEventRegisterKernelCapabilityClient:
7988 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
7989 if (!_joinedCapabilityClients) {
7990 _joinedCapabilityClients = OSSet::withCapacity(8);
7991 }
7992 if (arg0) {
7993 OSSharedPtr<IONotifier> notify((IONotifier *) arg0, OSNoRetain);
7994 if (_joinedCapabilityClients) {
7995 _joinedCapabilityClients->setObject(notify.get());
7996 synchronizePowerTree( kIOPMSyncNoChildNotify );
7997 }
7998 }
7999 break;
8000
8001 case kPowerEventPolicyStimulus:
8002 DMSG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8003 if (arg0) {
8004 int stimulus = (int)(uintptr_t) arg0;
8005 evaluatePolicy(stimulus, (uint32_t) arg1);
8006 }
8007 break;
8008
8009 case kPowerEventAssertionCreate:
8010 DMSG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8011 if (pmAssertions) {
8012 pmAssertions->handleCreateAssertion((OSValueObject<PMAssertStruct> *)arg0);
8013 }
8014 break;
8015
8016
8017 case kPowerEventAssertionRelease:
8018 DMSG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8019 if (pmAssertions) {
8020 pmAssertions->handleReleaseAssertion(arg1);
8021 }
8022 break;
8023
8024 case kPowerEventAssertionSetLevel:
8025 DMSG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8026 if (pmAssertions) {
8027 pmAssertions->handleSetAssertionLevel(arg1, (IOPMDriverAssertionLevel)(uintptr_t)arg0);
8028 }
8029 break;
8030
8031 case kPowerEventQueueSleepWakeUUID:
8032 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8033 handleQueueSleepWakeUUID((OSObject *)arg0);
8034 break;
8035 case kPowerEventPublishSleepWakeUUID:
8036 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8037 handlePublishSleepWakeUUID((bool)arg0);
8038 break;
8039
8040 case kPowerEventSetDisplayPowerOn:
8041 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8042 if (arg1 != 0) {
8043 displayPowerOnRequested = true;
8044 } else {
8045 displayPowerOnRequested = false;
8046 }
8047 handleSetDisplayPowerOn(displayPowerOnRequested);
8048 break;
8049
8050 case kPowerEventPublishWakeType:
8051 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8052
8053 // Don't replace wake type property if already set
8054 if ((arg0 == gIOPMWakeTypeUserKey) ||
8055 !propertyExists(kIOPMRootDomainWakeTypeKey)) {
8056 const char * wakeType = NULL;
8057
8058 if (arg0 == gIOPMWakeTypeUserKey) {
8059 requestUserActive(this, "WakeTypeUser");
8060 wakeType = kIOPMRootDomainWakeTypeUser;
8061 } else if (arg0 == gIOPMSettingDebugWakeRelativeKey) {
8062 requestUserActive(this, "WakeTypeAlarm");
8063 wakeType = kIOPMRootDomainWakeTypeAlarm;
8064 } else if (arg0 == gIOPMSettingSleepServiceWakeCalendarKey) {
8065 darkWakeSleepService = true;
8066 wakeType = kIOPMRootDomainWakeTypeSleepService;
8067 } else if (arg0 == gIOPMSettingMaintenanceWakeCalendarKey) {
8068 wakeType = kIOPMRootDomainWakeTypeMaintenance;
8069 }
8070
8071 if (wakeType) {
8072 setProperty(kIOPMRootDomainWakeTypeKey, wakeType);
8073 }
8074 }
8075 break;
8076
8077 case kPowerEventAOTEvaluate:
8078 DLOG("power event %u args %p 0x%llx\n", event, OBFUSCATE(arg0), arg1);
8079 if (_aotReadyToFullWake) {
8080 aotEvaluate(NULL);
8081 }
8082 break;
8083 }
8084 }
8085
8086 //******************************************************************************
8087 // systemPowerEventOccurred
8088 //
8089 // The power controller is notifying us of a hardware-related power management
8090 // event that we must handle.
8091 //
8092 // systemPowerEventOccurred covers the same functionality that
8093 // receivePowerNotification does; it simply provides a richer API for conveying
8094 // more information.
8095 //******************************************************************************
8096
8097 IOReturn
systemPowerEventOccurred(const OSSymbol * event,uint32_t intValue)8098 IOPMrootDomain::systemPowerEventOccurred(
8099 const OSSymbol *event,
8100 uint32_t intValue)
8101 {
8102 IOReturn attempt = kIOReturnSuccess;
8103 OSSharedPtr<OSNumber> newNumber;
8104
8105 if (!event) {
8106 return kIOReturnBadArgument;
8107 }
8108
8109 newNumber = OSNumber::withNumber(intValue, 8 * sizeof(intValue));
8110 if (!newNumber) {
8111 return kIOReturnInternalError;
8112 }
8113
8114 attempt = systemPowerEventOccurred(event, static_cast<OSObject *>(newNumber.get()));
8115
8116 return attempt;
8117 }
8118
8119 void
setThermalState(OSObject * value)8120 IOPMrootDomain::setThermalState(OSObject *value)
8121 {
8122 OSNumber * num;
8123
8124 if (gIOPMWorkLoop->inGate() == false) {
8125 gIOPMWorkLoop->runAction(
8126 OSMemberFunctionCast(IOWorkLoop::Action, this, &IOPMrootDomain::setThermalState),
8127 (OSObject *)this,
8128 (void *)value);
8129
8130 return;
8131 }
8132 if (value && (num = OSDynamicCast(OSNumber, value))) {
8133 thermalWarningState = ((num->unsigned32BitValue() == kIOPMThermalLevelWarning) ||
8134 (num->unsigned32BitValue() == kIOPMThermalLevelTrap)) ? 1 : 0;
8135 }
8136 }
8137
8138 IOReturn
systemPowerEventOccurred(const OSSymbol * event,OSObject * value)8139 IOPMrootDomain::systemPowerEventOccurred(
8140 const OSSymbol *event,
8141 OSObject *value)
8142 {
8143 OSSharedPtr<OSDictionary> thermalsDict;
8144 bool shouldUpdate = true;
8145
8146 if (!event || !value) {
8147 return kIOReturnBadArgument;
8148 }
8149
8150 // LOCK
8151 // We reuse featuresDict Lock because it already exists and guards
8152 // the very infrequently used publish/remove feature mechanism; so there's zero rsk
8153 // of stepping on that lock.
8154 if (featuresDictLock) {
8155 IOLockLock(featuresDictLock);
8156 }
8157
8158 OSSharedPtr<OSObject> origThermalsProp = copyProperty(kIOPMRootDomainPowerStatusKey);
8159 OSDictionary * origThermalsDict = OSDynamicCast(OSDictionary, origThermalsProp.get());
8160
8161 if (origThermalsDict) {
8162 thermalsDict = OSDictionary::withDictionary(origThermalsDict);
8163 } else {
8164 thermalsDict = OSDictionary::withCapacity(1);
8165 }
8166
8167 if (!thermalsDict) {
8168 shouldUpdate = false;
8169 goto exit;
8170 }
8171
8172 thermalsDict->setObject(event, value);
8173
8174 setProperty(kIOPMRootDomainPowerStatusKey, thermalsDict.get());
8175
8176 exit:
8177 // UNLOCK
8178 if (featuresDictLock) {
8179 IOLockUnlock(featuresDictLock);
8180 }
8181
8182 if (shouldUpdate) {
8183 if (event &&
8184 event->isEqualTo(kIOPMThermalLevelWarningKey)) {
8185 setThermalState(value);
8186 }
8187 messageClients(kIOPMMessageSystemPowerEventOccurred, (void *)NULL);
8188 }
8189
8190 return kIOReturnSuccess;
8191 }
8192
8193 //******************************************************************************
8194 // receivePowerNotification
8195 //
8196 // The power controller is notifying us of a hardware-related power management
8197 // event that we must handle. This may be a result of an 'environment' interrupt
8198 // from the power mgt micro.
8199 //******************************************************************************
8200
8201 IOReturn
receivePowerNotification(UInt32 msg)8202 IOPMrootDomain::receivePowerNotification( UInt32 msg )
8203 {
8204 if (msg & kIOPMPowerButton) {
8205 uint32_t currentPhase = pmTracer->getTracePhase();
8206 if (currentPhase != kIOPMTracePointSystemUp && currentPhase > kIOPMTracePointSystemSleep) {
8207 DEBUG_LOG("power button pressed during wake. phase = %u\n", currentPhase);
8208 swd_flags |= SWD_PWR_BTN_STACKSHOT;
8209 thread_call_enter(powerButtonDown);
8210 } else {
8211 DEBUG_LOG("power button pressed when system is up\n");
8212 }
8213 } else if (msg & kIOPMPowerButtonUp) {
8214 if (swd_flags & SWD_PWR_BTN_STACKSHOT) {
8215 swd_flags &= ~SWD_PWR_BTN_STACKSHOT;
8216 thread_call_enter(powerButtonUp);
8217 }
8218 } else {
8219 pmPowerStateQueue->submitPowerEvent(
8220 kPowerEventReceivedPowerNotification, (void *)(uintptr_t) msg );
8221 }
8222 return kIOReturnSuccess;
8223 }
8224
8225 void
handlePowerNotification(UInt32 msg)8226 IOPMrootDomain::handlePowerNotification( UInt32 msg )
8227 {
8228 bool eval_clamshell = false;
8229 bool eval_clamshell_alarm = false;
8230
8231 ASSERT_GATED();
8232
8233 /*
8234 * Local (IOPMrootDomain only) eval clamshell command
8235 */
8236 if (msg & kLocalEvalClamshellCommand) {
8237 if ((gClamshellFlags & kClamshell_WAR_47715679) && isRTCAlarmWake) {
8238 eval_clamshell_alarm = true;
8239
8240 // reset isRTCAlarmWake. This evaluation should happen only once
8241 // on RTC/Alarm wake. Any clamshell events after wake should follow
8242 // the regular evaluation
8243 isRTCAlarmWake = false;
8244 } else {
8245 eval_clamshell = true;
8246 }
8247 }
8248
8249 /*
8250 * Overtemp
8251 */
8252 if (msg & kIOPMOverTemp) {
8253 DLOG("Thermal overtemp message received!\n");
8254 thermalEmergencyState = true;
8255 privateSleepSystem(kIOPMSleepReasonThermalEmergency);
8256 }
8257
8258 /*
8259 * Forward DW thermal notification to client, if system is not going to sleep
8260 */
8261 if ((msg & kIOPMDWOverTemp) && (_systemTransitionType != kSystemTransitionSleep)) {
8262 DLOG("DarkWake thermal limits message received!\n");
8263 messageClients(kIOPMMessageDarkWakeThermalEmergency);
8264 }
8265
8266 /*
8267 * Sleep Now!
8268 */
8269 if (msg & kIOPMSleepNow) {
8270 privateSleepSystem(kIOPMSleepReasonSoftware);
8271 }
8272
8273 /*
8274 * Power Emergency
8275 */
8276 if (msg & kIOPMPowerEmergency) {
8277 DLOG("Received kIOPMPowerEmergency");
8278 lowBatteryCondition = true;
8279 privateSleepSystem(kIOPMSleepReasonLowPower);
8280 }
8281
8282 /*
8283 * Clamshell OPEN
8284 */
8285 if (msg & kIOPMClamshellOpened) {
8286 DLOG("Clamshell opened\n");
8287 // Received clamshel open message from clamshell controlling driver
8288 // Update our internal state and tell general interest clients
8289 clamshellClosed = false;
8290 clamshellExists = true;
8291
8292 // Don't issue a hid tickle when lid is open and polled on wake
8293 if (msg & kIOPMSetValue) {
8294 setProperty(kIOPMRootDomainWakeTypeKey, "Lid Open");
8295 reportUserInput();
8296 }
8297
8298 // Tell PMCPU
8299 informCPUStateChange(kInformLid, 0);
8300
8301 // Tell general interest clients
8302 sendClientClamshellNotification();
8303
8304 bool aborting = ((lastSleepReason == kIOPMSleepReasonClamshell)
8305 || (lastSleepReason == kIOPMSleepReasonIdle)
8306 || (lastSleepReason == kIOPMSleepReasonMaintenance));
8307 if (aborting) {
8308 userActivityCount++;
8309 }
8310 DLOG("clamshell tickled %d lastSleepReason %d\n", userActivityCount, lastSleepReason);
8311 }
8312
8313 /*
8314 * Clamshell CLOSED
8315 * Send the clamshell interest notification since the lid is closing.
8316 */
8317 if (msg & kIOPMClamshellClosed) {
8318 if ((clamshellIgnoreClose || (gClamshellFlags & kClamshell_WAR_38378787)) &&
8319 clamshellClosed && clamshellExists) {
8320 DLOG("Ignoring redundant Clamshell close event\n");
8321 } else {
8322 DLOG("Clamshell closed\n");
8323 // Received clamshel open message from clamshell controlling driver
8324 // Update our internal state and tell general interest clients
8325 clamshellClosed = true;
8326 clamshellExists = true;
8327
8328 // Ignore all following clamshell close events until the clamshell
8329 // is opened or the system sleeps. When a clamshell close triggers
8330 // a system wake, the lid driver may send us two clamshell close
8331 // events, one for the clamshell close event itself, and a second
8332 // close event when the driver polls the lid state on wake.
8333 clamshellIgnoreClose = true;
8334
8335 // Tell PMCPU
8336 informCPUStateChange(kInformLid, 1);
8337
8338 // Tell general interest clients
8339 sendClientClamshellNotification();
8340
8341 // And set eval_clamshell = so we can attempt
8342 eval_clamshell = true;
8343 }
8344 }
8345
8346 /*
8347 * Set Desktop mode (sent from graphics)
8348 *
8349 * -> reevaluate lid state
8350 */
8351 if (msg & kIOPMSetDesktopMode) {
8352 desktopMode = (0 != (msg & kIOPMSetValue));
8353 msg &= ~(kIOPMSetDesktopMode | kIOPMSetValue);
8354 DLOG("Desktop mode %d\n", desktopMode);
8355
8356 sendClientClamshellNotification();
8357
8358 // Re-evaluate the lid state
8359 eval_clamshell = true;
8360 }
8361
8362 /*
8363 * AC Adaptor connected
8364 *
8365 * -> reevaluate lid state
8366 */
8367 if (msg & kIOPMSetACAdaptorConnected) {
8368 acAdaptorConnected = (0 != (msg & kIOPMSetValue));
8369 msg &= ~(kIOPMSetACAdaptorConnected | kIOPMSetValue);
8370
8371 // Tell CPU PM
8372 informCPUStateChange(kInformAC, !acAdaptorConnected);
8373
8374 // Tell BSD if AC is connected
8375 // 0 == external power source; 1 == on battery
8376 post_sys_powersource(acAdaptorConnected ? 0:1);
8377
8378 sendClientClamshellNotification();
8379
8380 IOUserServer::powerSourceChanged(acAdaptorConnected);
8381
8382 // Re-evaluate the lid state
8383 eval_clamshell = true;
8384
8385 // Lack of AC may have latched a display wrangler tickle.
8386 // This mirrors the hardware's USB wake event latch, where a latched
8387 // USB wake event followed by an AC attach will trigger a full wake.
8388 latchDisplayWranglerTickle( false );
8389
8390 #if HIBERNATION
8391 // AC presence will reset the standy timer delay adjustment.
8392 _standbyTimerResetSeconds = 0;
8393 #endif
8394 if (!userIsActive) {
8395 // Reset userActivityTime when power supply is changed(rdr 13789330)
8396 clock_get_uptime(&userActivityTime);
8397 }
8398 }
8399
8400 /*
8401 * Enable Clamshell (external display disappear)
8402 *
8403 * -> reevaluate lid state
8404 */
8405 if (msg & kIOPMEnableClamshell) {
8406 DLOG("Clamshell enabled\n");
8407
8408 // Re-evaluate the lid state
8409 // System should sleep on external display disappearance
8410 // in lid closed operation.
8411 if (true == clamshellDisabled) {
8412 eval_clamshell = true;
8413
8414 #if DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY
8415 // Also clear kClamshellSleepDisableInternal when graphics enables
8416 // the clamshell during a full wake. When graphics is behaving as
8417 // expected, this will allow clamshell close to be honored earlier
8418 // rather than waiting for the delayed evaluation.
8419 if ((clamshellSleepDisableMask & kClamshellSleepDisableInternal) &&
8420 (CAP_PENDING(kIOPMSystemCapabilityGraphics) ||
8421 CAP_CURRENT(kIOPMSystemCapabilityGraphics))) {
8422 setClamShellSleepDisable(false, kClamshellSleepDisableInternal);
8423
8424 // Cancel the TC to avoid an extra kLocalEvalClamshellCommand
8425 // when timer expires which is harmless but useless.
8426 thread_call_cancel(fullWakeThreadCall);
8427 }
8428 #endif
8429 }
8430
8431 clamshellDisabled = false;
8432 sendClientClamshellNotification();
8433 }
8434
8435 /*
8436 * Disable Clamshell (external display appeared)
8437 * We don't bother re-evaluating clamshell state. If the system is awake,
8438 * the lid is probably open.
8439 */
8440 if (msg & kIOPMDisableClamshell) {
8441 DLOG("Clamshell disabled\n");
8442 clamshellDisabled = true;
8443 sendClientClamshellNotification();
8444 }
8445
8446 /*
8447 * Evaluate clamshell and SLEEP if appropriate
8448 */
8449 if (eval_clamshell_alarm && clamshellClosed) {
8450 if (shouldSleepOnRTCAlarmWake()) {
8451 privateSleepSystem(kIOPMSleepReasonClamshell);
8452 }
8453 } else if (eval_clamshell && clamshellClosed) {
8454 if (shouldSleepOnClamshellClosed()) {
8455 privateSleepSystem(kIOPMSleepReasonClamshell);
8456 } else {
8457 evaluatePolicy( kStimulusDarkWakeEvaluate );
8458 }
8459 }
8460
8461 if (msg & kIOPMProModeEngaged) {
8462 int newState = 1;
8463 DLOG("ProModeEngaged\n");
8464 messageClient(kIOPMMessageProModeStateChange, systemCapabilityNotifier.get(), &newState, sizeof(newState));
8465 }
8466
8467 if (msg & kIOPMProModeDisengaged) {
8468 int newState = 0;
8469 DLOG("ProModeDisengaged\n");
8470 messageClient(kIOPMMessageProModeStateChange, systemCapabilityNotifier.get(), &newState, sizeof(newState));
8471 }
8472 }
8473
8474 //******************************************************************************
8475 // evaluatePolicy
8476 //
8477 // Evaluate root-domain policy in response to external changes.
8478 //******************************************************************************
8479
8480 void
evaluatePolicy(int stimulus,uint32_t arg)8481 IOPMrootDomain::evaluatePolicy( int stimulus, uint32_t arg )
8482 {
8483 union {
8484 struct {
8485 int idleSleepEnabled : 1;
8486 int idleSleepDisabled : 1;
8487 int displaySleep : 1;
8488 int sleepDelayChanged : 1;
8489 int evaluateDarkWake : 1;
8490 int adjustPowerState : 1;
8491 int userBecameInactive : 1;
8492 int displaySleepEntry : 1;
8493 } bit;
8494 uint32_t u32;
8495 } flags;
8496
8497
8498 ASSERT_GATED();
8499 flags.u32 = 0;
8500
8501 switch (stimulus) {
8502 case kStimulusDisplayWranglerSleep:
8503 DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8504 if (!wranglerPowerOff) {
8505 // wrangler is in sleep state or lower
8506 flags.bit.displaySleep = true;
8507 }
8508 if (!wranglerAsleep) {
8509 // transition from wrangler wake to wrangler sleep
8510 flags.bit.displaySleepEntry = true;
8511 wranglerAsleep = true;
8512 }
8513 break;
8514
8515 case kStimulusDisplayWranglerWake:
8516 DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8517 displayIdleForDemandSleep = false;
8518 wranglerPowerOff = false;
8519 wranglerAsleep = false;
8520 break;
8521
8522 case kStimulusEnterUserActiveState:
8523 DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8524 if (_preventUserActive) {
8525 DLOG("user active dropped\n");
8526 break;
8527 }
8528 if (!userIsActive) {
8529 userIsActive = true;
8530 userWasActive = true;
8531 clock_get_uptime(&gUserActiveAbsTime);
8532
8533 // Stay awake after dropping demand for display power on
8534 if (kFullWakeReasonDisplayOn == fullWakeReason) {
8535 fullWakeReason = fFullWakeReasonDisplayOnAndLocalUser;
8536 DLOG("User activity while in notification wake\n");
8537 changePowerStateWithOverrideTo( getRUN_STATE(), 0);
8538 }
8539
8540 kdebugTrace(kPMLogUserActiveState, 0, 1, 0);
8541 setProperty(gIOPMUserIsActiveKey.get(), kOSBooleanTrue);
8542 messageClients(kIOPMMessageUserIsActiveChanged);
8543 }
8544 flags.bit.idleSleepDisabled = true;
8545 break;
8546
8547 case kStimulusLeaveUserActiveState:
8548 DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8549 if (userIsActive) {
8550 clock_get_uptime(&gUserInactiveAbsTime);
8551 userIsActive = false;
8552 clock_get_uptime(&userBecameInactiveTime);
8553 flags.bit.userBecameInactive = true;
8554
8555 kdebugTrace(kPMLogUserActiveState, 0, 0, 0);
8556 setProperty(gIOPMUserIsActiveKey.get(), kOSBooleanFalse);
8557 messageClients(kIOPMMessageUserIsActiveChanged);
8558 }
8559 break;
8560
8561 case kStimulusAggressivenessChanged:
8562 {
8563 DMSG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8564 unsigned long aggressiveValue;
8565 uint32_t minutesToIdleSleep = 0;
8566 uint32_t minutesToDisplayDim = 0;
8567 uint32_t minutesDelta = 0;
8568
8569 // Fetch latest display and system sleep slider values.
8570 aggressiveValue = 0;
8571 getAggressiveness(kPMMinutesToSleep, &aggressiveValue);
8572 minutesToIdleSleep = (uint32_t) aggressiveValue;
8573
8574 aggressiveValue = 0;
8575 getAggressiveness(kPMMinutesToDim, &aggressiveValue);
8576 minutesToDisplayDim = (uint32_t) aggressiveValue;
8577 DLOG("aggressiveness changed: system %u->%u, display %u\n",
8578 sleepSlider, minutesToIdleSleep, minutesToDisplayDim);
8579
8580 DLOG("idle time -> %d ms (ena %d)\n",
8581 idleMilliSeconds, (minutesToIdleSleep != 0));
8582
8583 // How long to wait before sleeping the system once
8584 // the displays turns off is indicated by 'extraSleepDelay'.
8585
8586 if (minutesToIdleSleep > minutesToDisplayDim) {
8587 minutesDelta = minutesToIdleSleep - minutesToDisplayDim;
8588 } else if (minutesToIdleSleep == minutesToDisplayDim) {
8589 minutesDelta = 1;
8590 }
8591
8592 if ((!idleSleepEnabled) && (minutesToIdleSleep != 0)) {
8593 idleSleepEnabled = flags.bit.idleSleepEnabled = true;
8594 }
8595
8596 if ((idleSleepEnabled) && (minutesToIdleSleep == 0)) {
8597 flags.bit.idleSleepDisabled = true;
8598 idleSleepEnabled = false;
8599 }
8600 #if !defined(XNU_TARGET_OS_OSX)
8601 if (0x7fffffff == minutesToIdleSleep) {
8602 minutesToIdleSleep = idleMilliSeconds / 1000;
8603 }
8604 #endif /* !defined(XNU_TARGET_OS_OSX) */
8605
8606 if (((minutesDelta != extraSleepDelay) ||
8607 (userActivityTime != userActivityTime_prev)) &&
8608 !flags.bit.idleSleepEnabled && !flags.bit.idleSleepDisabled) {
8609 flags.bit.sleepDelayChanged = true;
8610 }
8611
8612 if (systemDarkWake && !darkWakeToSleepASAP &&
8613 (flags.bit.idleSleepEnabled || flags.bit.idleSleepDisabled)) {
8614 // Reconsider decision to remain in dark wake
8615 flags.bit.evaluateDarkWake = true;
8616 }
8617
8618 sleepSlider = minutesToIdleSleep;
8619 extraSleepDelay = minutesDelta;
8620 userActivityTime_prev = userActivityTime;
8621 } break;
8622
8623 case kStimulusDemandSystemSleep:
8624 DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8625 displayIdleForDemandSleep = true;
8626 if (wrangler && wranglerIdleSettings) {
8627 // Request wrangler idle only when demand sleep is triggered
8628 // from full wake.
8629 if (CAP_CURRENT(kIOPMSystemCapabilityGraphics)) {
8630 wrangler->setProperties(wranglerIdleSettings.get());
8631 DLOG("Requested wrangler idle\n");
8632 }
8633 }
8634 // arg = sleepReason
8635 changePowerStateWithOverrideTo( SLEEP_STATE, arg );
8636 break;
8637
8638 case kStimulusAllowSystemSleepChanged:
8639 DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8640 flags.bit.adjustPowerState = true;
8641 break;
8642
8643 case kStimulusDarkWakeActivityTickle:
8644 DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8645 // arg == true implies real and not self generated wrangler tickle.
8646 // Update wake type on PM work loop instead of the tickle thread to
8647 // eliminate the possibility of an early tickle clobbering the wake
8648 // type set by the platform driver.
8649 if (arg == true) {
8650 setProperty(kIOPMRootDomainWakeTypeKey, kIOPMRootDomainWakeTypeHIDActivity);
8651 }
8652
8653 if (!darkWakeExit) {
8654 if (latchDisplayWranglerTickle(true)) {
8655 DLOG("latched tickle\n");
8656 break;
8657 }
8658
8659 darkWakeExit = true;
8660 DLOG("Requesting full wake due to dark wake activity tickle\n");
8661 requestFullWake( kFullWakeReasonLocalUser );
8662 }
8663 break;
8664
8665 case kStimulusDarkWakeEntry:
8666 case kStimulusDarkWakeReentry:
8667 DLOG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8668 // Any system transitions since the last dark wake transition
8669 // will invalid the stimulus.
8670
8671 if (arg == _systemStateGeneration) {
8672 DLOG("dark wake entry\n");
8673 systemDarkWake = true;
8674
8675 // Keep wranglerPowerOff an invariant when wrangler is absent
8676 if (wrangler) {
8677 wranglerPowerOff = true;
8678 }
8679
8680 if (kStimulusDarkWakeEntry == stimulus) {
8681 clock_get_uptime(&userBecameInactiveTime);
8682 flags.bit.evaluateDarkWake = true;
8683 if (activitySinceSleep()) {
8684 DLOG("User activity recorded while going to darkwake\n");
8685 reportUserInput();
8686 }
8687 }
8688
8689 // Always accelerate disk spindown while in dark wake,
8690 // even if system does not support/allow sleep.
8691
8692 cancelIdleSleepTimer();
8693 setQuickSpinDownTimeout();
8694 }
8695 break;
8696
8697 case kStimulusDarkWakeEvaluate:
8698 DMSG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8699 if (systemDarkWake) {
8700 flags.bit.evaluateDarkWake = true;
8701 }
8702 break;
8703
8704 case kStimulusNoIdleSleepPreventers:
8705 DMSG("evaluatePolicy( %d, 0x%x )\n", stimulus, arg);
8706 flags.bit.adjustPowerState = true;
8707 break;
8708 } /* switch(stimulus) */
8709
8710 if (flags.bit.evaluateDarkWake && (kFullWakeReasonNone == fullWakeReason)) {
8711 DLOG("DarkWake: sleepASAP %d, clamshell closed %d, disabled %d/%x, desktopMode %d, ac %d\n",
8712 darkWakeToSleepASAP, clamshellClosed, clamshellDisabled, clamshellSleepDisableMask, desktopMode, acAdaptorConnected);
8713 if (darkWakeToSleepASAP ||
8714 (clamshellClosed && !(desktopMode && acAdaptorConnected))) {
8715 uint32_t newSleepReason;
8716
8717 if (CAP_HIGHEST(kIOPMSystemCapabilityGraphics)) {
8718 // System was previously in full wake. Sleep reason from
8719 // full to dark already recorded in fullToDarkReason.
8720
8721 if (lowBatteryCondition) {
8722 newSleepReason = kIOPMSleepReasonLowPower;
8723 } else if (thermalEmergencyState) {
8724 newSleepReason = kIOPMSleepReasonThermalEmergency;
8725 } else {
8726 newSleepReason = fullToDarkReason;
8727 }
8728 } else {
8729 // In dark wake from system sleep.
8730
8731 if (darkWakeSleepService) {
8732 newSleepReason = kIOPMSleepReasonSleepServiceExit;
8733 } else {
8734 newSleepReason = kIOPMSleepReasonMaintenance;
8735 }
8736 }
8737
8738 if (checkSystemCanSleep(newSleepReason)) {
8739 privateSleepSystem(newSleepReason);
8740 }
8741 } else { // non-maintenance (network) dark wake
8742 if (checkSystemCanSleep(kIOPMSleepReasonIdle)) {
8743 // Release power clamp, and wait for children idle.
8744 adjustPowerState(true);
8745 } else {
8746 changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonDarkWakeCannotSleep);
8747 }
8748 }
8749 }
8750
8751 if (systemDarkWake) {
8752 // The rest are irrelevant while system is in dark wake.
8753 flags.u32 = 0;
8754 }
8755
8756 if ((flags.bit.displaySleepEntry) &&
8757 (kFullWakeReasonDisplayOn == fullWakeReason)) {
8758 // kIOPMSleepReasonNotificationWakeExit
8759 DLOG("Display sleep while in notification wake\n");
8760 changePowerStateWithOverrideTo(SLEEP_STATE, kIOPMSleepReasonNotificationWakeExit);
8761 }
8762
8763 if (flags.bit.userBecameInactive || flags.bit.sleepDelayChanged) {
8764 bool cancelQuickSpindown = false;
8765
8766 if (flags.bit.sleepDelayChanged) {
8767 // Cancel existing idle sleep timer and quick disk spindown.
8768 // New settings will be applied by the idleSleepEnabled flag
8769 // handler below if idle sleep is enabled.
8770
8771 DLOG("extra sleep timer changed\n");
8772 cancelIdleSleepTimer();
8773 cancelQuickSpindown = true;
8774 } else {
8775 DLOG("user inactive\n");
8776 }
8777
8778 if (!userIsActive && idleSleepEnabled) {
8779 startIdleSleepTimer(getTimeToIdleSleep());
8780 }
8781
8782 if (cancelQuickSpindown) {
8783 restoreUserSpinDownTimeout();
8784 }
8785 }
8786
8787 if (flags.bit.idleSleepEnabled) {
8788 DLOG("idle sleep timer enabled\n");
8789 if (!wrangler) {
8790 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
8791 startIdleSleepTimer(getTimeToIdleSleep());
8792 #else
8793 changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonIdleSleepEnabled);
8794 startIdleSleepTimer( idleMilliSeconds );
8795 #endif
8796 } else {
8797 // Start idle timer if prefs now allow system sleep
8798 // and user is already inactive. Disk spindown is
8799 // accelerated upon timer expiration.
8800
8801 if (!userIsActive) {
8802 startIdleSleepTimer(getTimeToIdleSleep());
8803 }
8804 }
8805 }
8806
8807 if (flags.bit.idleSleepDisabled) {
8808 DLOG("idle sleep timer disabled\n");
8809 cancelIdleSleepTimer();
8810 restoreUserSpinDownTimeout();
8811 adjustPowerState();
8812 }
8813
8814 if (flags.bit.adjustPowerState) {
8815 bool sleepASAP = false;
8816
8817 if (!systemBooting && (0 == idleSleepPreventersCount())) {
8818 if (!wrangler) {
8819 changePowerStateWithTagToPriv(getRUN_STATE(), kCPSReasonEvaluatePolicy);
8820 if (idleSleepEnabled) {
8821 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
8822 if (!extraSleepDelay && !idleSleepTimerPending && !gNoIdleFlag) {
8823 sleepASAP = true;
8824 }
8825 #else
8826 // stay awake for at least idleMilliSeconds
8827 startIdleSleepTimer(idleMilliSeconds);
8828 #endif
8829 }
8830 } else if (!extraSleepDelay && !idleSleepTimerPending && !systemDarkWake && !gNoIdleFlag) {
8831 sleepASAP = true;
8832 }
8833 }
8834
8835 adjustPowerState(sleepASAP);
8836 }
8837 }
8838
8839 //******************************************************************************
8840
8841 unsigned int
idleSleepPreventersCount()8842 IOPMrootDomain::idleSleepPreventersCount()
8843 {
8844 if (_aotMode) {
8845 unsigned int count __block;
8846 count = 0;
8847 preventIdleSleepList->iterateObjects(^bool (OSObject * obj)
8848 {
8849 count += (NULL == obj->metaCast("AppleARMBacklight"));
8850 return false;
8851 });
8852 return count;
8853 }
8854
8855 return preventIdleSleepList->getCount();
8856 }
8857
8858
8859 //******************************************************************************
8860 // requestFullWake
8861 //
8862 // Request transition from dark wake to full wake
8863 //******************************************************************************
8864
8865 void
requestFullWake(FullWakeReason reason)8866 IOPMrootDomain::requestFullWake( FullWakeReason reason )
8867 {
8868 uint32_t options = 0;
8869 IOService * pciRoot = NULL;
8870 bool promotion = false;
8871
8872 // System must be in dark wake and a valid reason for entering full wake
8873 if ((kFullWakeReasonNone == reason) ||
8874 (kFullWakeReasonNone != fullWakeReason) ||
8875 (CAP_CURRENT(kIOPMSystemCapabilityGraphics))) {
8876 return;
8877 }
8878
8879 // Will clear reason upon exit from full wake
8880 fullWakeReason = reason;
8881
8882 _desiredCapability |= (kIOPMSystemCapabilityGraphics |
8883 kIOPMSystemCapabilityAudio);
8884
8885 if ((kSystemTransitionWake == _systemTransitionType) &&
8886 !(_pendingCapability & kIOPMSystemCapabilityGraphics) &&
8887 !darkWakePowerClamped) {
8888 // Promote to full wake while waking up to dark wake due to tickle.
8889 // PM will hold off notifying the graphics subsystem about system wake
8890 // as late as possible, so if a HID tickle does arrive, graphics can
8891 // power up from this same wake transition. Otherwise, the latency to
8892 // power up graphics on the following transition can be huge on certain
8893 // systems. However, once any power clamping has taken effect, it is
8894 // too late to promote the current dark wake transition to a full wake.
8895 _pendingCapability |= (kIOPMSystemCapabilityGraphics |
8896 kIOPMSystemCapabilityAudio);
8897
8898 // Tell the PCI parent of audio and graphics drivers to stop
8899 // delaying the child notifications. Same for root domain.
8900 pciRoot = pciHostBridgeDriver.get();
8901 willEnterFullWake();
8902 promotion = true;
8903 }
8904
8905 // Unsafe to cancel once graphics was powered.
8906 // If system woke from dark wake, the return to sleep can
8907 // be cancelled. "awake -> dark -> sleep" transition
8908 // can be cancelled also, during the "dark -> sleep" phase
8909 // *prior* to driver power down.
8910 if (!CAP_HIGHEST(kIOPMSystemCapabilityGraphics) ||
8911 _pendingCapability == 0) {
8912 options |= kIOPMSyncCancelPowerDown;
8913 }
8914
8915 synchronizePowerTree(options, pciRoot);
8916
8917 if (kFullWakeReasonLocalUser == fullWakeReason) {
8918 // IOGraphics doesn't light the display even though graphics is
8919 // enabled in kIOMessageSystemCapabilityChange message(radar 9502104)
8920 // So, do an explicit activity tickle
8921 if (wrangler) {
8922 wrangler->activityTickle(0, 0);
8923 }
8924 }
8925
8926 // Log a timestamp for the initial full wake request.
8927 // System may not always honor this full wake request.
8928 if (!CAP_HIGHEST(kIOPMSystemCapabilityGraphics)) {
8929 AbsoluteTime now;
8930 uint64_t nsec;
8931
8932 clock_get_uptime(&now);
8933 SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
8934 absolutetime_to_nanoseconds(now, &nsec);
8935 MSG("full wake %s (reason %u) %u ms\n",
8936 promotion ? "promotion" : "request",
8937 fullWakeReason, ((int)((nsec) / NSEC_PER_MSEC)));
8938 }
8939 }
8940
8941 //******************************************************************************
8942 // willEnterFullWake
8943 //
8944 // System will enter full wake from sleep, from dark wake, or from dark
8945 // wake promotion. This function aggregate things that are in common to
8946 // all three full wake transitions.
8947 //
8948 // Assumptions: fullWakeReason was updated
8949 //******************************************************************************
8950
8951 void
willEnterFullWake(void)8952 IOPMrootDomain::willEnterFullWake( void )
8953 {
8954 hibernateRetry = false;
8955 sleepToStandby = false;
8956 standbyNixed = false;
8957 resetTimers = false;
8958 sleepTimerMaintenance = false;
8959
8960 assert(!CAP_CURRENT(kIOPMSystemCapabilityGraphics));
8961
8962 _systemMessageClientMask = kSystemMessageClientPowerd |
8963 kSystemMessageClientLegacyApp;
8964
8965 if ((_highestCapability & kIOPMSystemCapabilityGraphics) == 0) {
8966 // First time to attain full wake capability since the last wake
8967 _systemMessageClientMask |= kSystemMessageClientKernel;
8968
8969 // Set kIOPMUserTriggeredFullWakeKey before full wake for IOGraphics
8970 setProperty(gIOPMUserTriggeredFullWakeKey.get(),
8971 (kFullWakeReasonLocalUser == fullWakeReason) ?
8972 kOSBooleanTrue : kOSBooleanFalse);
8973 }
8974 #if HIBERNATION
8975 IOHibernateSetWakeCapabilities(_pendingCapability);
8976 #endif
8977
8978 IOService::setAdvisoryTickleEnable( true );
8979 tellClients(kIOMessageSystemWillPowerOn);
8980 preventTransitionToUserActive(false);
8981 }
8982
8983 //******************************************************************************
8984 // fullWakeDelayedWork
8985 //
8986 // System has already entered full wake. Invoked by a delayed thread call.
8987 //******************************************************************************
8988
8989 void
fullWakeDelayedWork(void)8990 IOPMrootDomain::fullWakeDelayedWork( void )
8991 {
8992 #if DARK_TO_FULL_EVALUATE_CLAMSHELL_DELAY
8993 if (!gIOPMWorkLoop->inGate()) {
8994 gIOPMWorkLoop->runAction(
8995 OSMemberFunctionCast(IOWorkLoop::Action, this,
8996 &IOPMrootDomain::fullWakeDelayedWork), this);
8997 return;
8998 }
8999
9000 DLOG("fullWakeDelayedWork cap cur %x pend %x high %x, clamshell disable %x/%x\n",
9001 _currentCapability, _pendingCapability, _highestCapability,
9002 clamshellDisabled, clamshellSleepDisableMask);
9003
9004 if (clamshellExists &&
9005 CAP_CURRENT(kIOPMSystemCapabilityGraphics) &&
9006 !CAP_CHANGE(kIOPMSystemCapabilityGraphics)) {
9007 if (clamshellSleepDisableMask & kClamshellSleepDisableInternal) {
9008 setClamShellSleepDisable(false, kClamshellSleepDisableInternal);
9009 } else {
9010 // Not the initial full wake after waking from sleep.
9011 // Evaluate the clamshell for rdar://problem/9157444.
9012 receivePowerNotification(kLocalEvalClamshellCommand);
9013 }
9014 }
9015 #endif
9016 }
9017
9018 //******************************************************************************
9019 // evaluateAssertions
9020 //
9021 //******************************************************************************
9022
9023 // Bitmask of all kernel assertions that prevent system idle sleep.
9024 // kIOPMDriverAssertionReservedBit7 is reserved for IOMediaBSDClient.
9025 #define NO_IDLE_SLEEP_ASSERTIONS_MASK \
9026 (kIOPMDriverAssertionReservedBit7 | \
9027 kIOPMDriverAssertionPreventSystemIdleSleepBit)
9028
9029 void
evaluateAssertions(IOPMDriverAssertionType newAssertions,IOPMDriverAssertionType oldAssertions)9030 IOPMrootDomain::evaluateAssertions(IOPMDriverAssertionType newAssertions, IOPMDriverAssertionType oldAssertions)
9031 {
9032 IOPMDriverAssertionType changedBits = newAssertions ^ oldAssertions;
9033
9034 messageClients(kIOPMMessageDriverAssertionsChanged);
9035
9036 if (changedBits & kIOPMDriverAssertionPreventDisplaySleepBit) {
9037 if (wrangler) {
9038 bool value = (newAssertions & kIOPMDriverAssertionPreventDisplaySleepBit) ? true : false;
9039
9040 DLOG("wrangler->setIgnoreIdleTimer\(%d)\n", value);
9041 wrangler->setIgnoreIdleTimer( value );
9042 }
9043 }
9044
9045 if (changedBits & kIOPMDriverAssertionCPUBit) {
9046 if (_aotNow) {
9047 IOLog("CPU assertions %d\n", (0 != (kIOPMDriverAssertionCPUBit & newAssertions)));
9048 }
9049 evaluatePolicy(_aotNow ? kStimulusNoIdleSleepPreventers : kStimulusDarkWakeEvaluate);
9050 if (!assertOnWakeSecs && gIOLastWakeAbsTime) {
9051 AbsoluteTime now;
9052 clock_usec_t microsecs;
9053 clock_get_uptime(&now);
9054 SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
9055 absolutetime_to_microtime(now, &assertOnWakeSecs, µsecs);
9056 if (assertOnWakeReport) {
9057 HISTREPORT_TALLYVALUE(assertOnWakeReport, (int64_t)assertOnWakeSecs);
9058 DLOG("Updated assertOnWake %lu\n", (unsigned long)assertOnWakeSecs);
9059 }
9060 }
9061 }
9062
9063 if (changedBits & NO_IDLE_SLEEP_ASSERTIONS_MASK) {
9064 if ((newAssertions & NO_IDLE_SLEEP_ASSERTIONS_MASK) != 0) {
9065 if ((oldAssertions & NO_IDLE_SLEEP_ASSERTIONS_MASK) == 0) {
9066 DLOG("PreventIdleSleep driver assertion raised\n");
9067 bool ok = updatePreventIdleSleepList(this, true);
9068 if (ok && (changedBits & kIOPMDriverAssertionPreventSystemIdleSleepBit)) {
9069 // Cancel idle sleep if there is one in progress
9070 cancelIdlePowerDown(this);
9071 }
9072 }
9073 } else {
9074 DLOG("PreventIdleSleep driver assertion dropped\n");
9075 updatePreventIdleSleepList(this, false);
9076 }
9077 }
9078 }
9079
9080 // MARK: -
9081 // MARK: Statistics
9082
9083 //******************************************************************************
9084 // pmStats
9085 //
9086 //******************************************************************************
9087
9088 void
pmStatsRecordEvent(int eventIndex,AbsoluteTime timestamp)9089 IOPMrootDomain::pmStatsRecordEvent(
9090 int eventIndex,
9091 AbsoluteTime timestamp)
9092 {
9093 bool starting = eventIndex & kIOPMStatsEventStartFlag ? true:false;
9094 bool stopping = eventIndex & kIOPMStatsEventStopFlag ? true:false;
9095 uint64_t delta;
9096 uint64_t nsec;
9097 OSSharedPtr<OSData> publishPMStats;
9098
9099 eventIndex &= ~(kIOPMStatsEventStartFlag | kIOPMStatsEventStopFlag);
9100
9101 absolutetime_to_nanoseconds(timestamp, &nsec);
9102
9103 switch (eventIndex) {
9104 case kIOPMStatsHibernateImageWrite:
9105 if (starting) {
9106 gPMStats.hibWrite.start = nsec;
9107 } else if (stopping) {
9108 gPMStats.hibWrite.stop = nsec;
9109 }
9110
9111 if (stopping) {
9112 delta = gPMStats.hibWrite.stop - gPMStats.hibWrite.start;
9113 IOLog("PMStats: Hibernate write took %qd ms\n", delta / NSEC_PER_MSEC);
9114 }
9115 break;
9116 case kIOPMStatsHibernateImageRead:
9117 if (starting) {
9118 gPMStats.hibRead.start = nsec;
9119 } else if (stopping) {
9120 gPMStats.hibRead.stop = nsec;
9121 }
9122
9123 if (stopping) {
9124 delta = gPMStats.hibRead.stop - gPMStats.hibRead.start;
9125 IOLog("PMStats: Hibernate read took %qd ms\n", delta / NSEC_PER_MSEC);
9126
9127 publishPMStats = OSData::withValue(gPMStats);
9128 setProperty(kIOPMSleepStatisticsKey, publishPMStats.get());
9129 bzero(&gPMStats, sizeof(gPMStats));
9130 }
9131 break;
9132 }
9133 }
9134
9135 /*
9136 * Appends a record of the application response to
9137 * IOPMrootDomain::pmStatsAppResponses
9138 */
9139 void
pmStatsRecordApplicationResponse(const OSSymbol * response,const char * name,int messageType,uint32_t delay_ms,uint64_t id,OSObject * object,IOPMPowerStateIndex powerState,bool async)9140 IOPMrootDomain::pmStatsRecordApplicationResponse(
9141 const OSSymbol *response,
9142 const char *name,
9143 int messageType,
9144 uint32_t delay_ms,
9145 uint64_t id,
9146 OSObject *object,
9147 IOPMPowerStateIndex powerState,
9148 bool async)
9149 {
9150 OSSharedPtr<OSDictionary> responseDescription;
9151 OSSharedPtr<OSNumber> delayNum;
9152 OSSharedPtr<OSNumber> powerCaps;
9153 OSSharedPtr<OSNumber> pidNum;
9154 OSSharedPtr<OSNumber> msgNum;
9155 OSSharedPtr<const OSSymbol> appname;
9156 OSSharedPtr<const OSSymbol> sleep;
9157 OSSharedPtr<const OSSymbol> wake;
9158 IOPMServiceInterestNotifier *notify = NULL;
9159
9160 if (object && (notify = OSDynamicCast(IOPMServiceInterestNotifier, object))) {
9161 if (response->isEqualTo(gIOPMStatsResponseTimedOut.get())) {
9162 notify->ackTimeoutCnt++;
9163 } else {
9164 notify->ackTimeoutCnt = 0;
9165 }
9166 }
9167
9168 if (response->isEqualTo(gIOPMStatsResponsePrompt.get()) ||
9169 (_systemTransitionType == kSystemTransitionNone) || (_systemTransitionType == kSystemTransitionNewCapClient)) {
9170 return;
9171 }
9172
9173
9174 if (response->isEqualTo(gIOPMStatsDriverPSChangeSlow.get())) {
9175 kdebugTrace(kPMLogDrvPSChangeDelay, id, messageType, delay_ms);
9176 } else if (notify) {
9177 // User space app or kernel capability client
9178 if (id) {
9179 kdebugTrace(kPMLogAppResponseDelay, id, notify->msgType, delay_ms);
9180 } else {
9181 kdebugTrace(kPMLogDrvResponseDelay, notify->uuid0, messageType, delay_ms);
9182 }
9183 notify->msgType = 0;
9184 }
9185
9186 responseDescription = OSDictionary::withCapacity(5);
9187 if (responseDescription) {
9188 if (response) {
9189 responseDescription->setObject(_statsResponseTypeKey.get(), response);
9190 }
9191
9192 msgNum = OSNumber::withNumber(messageType, 32);
9193 if (msgNum) {
9194 responseDescription->setObject(_statsMessageTypeKey.get(), msgNum.get());
9195 }
9196
9197 if (!name && notify && notify->identifier) {
9198 name = notify->identifier->getCStringNoCopy();
9199 }
9200
9201 if (name && (strlen(name) > 0)) {
9202 appname = OSSymbol::withCString(name);
9203 if (appname) {
9204 responseDescription->setObject(_statsNameKey.get(), appname.get());
9205 }
9206 }
9207
9208 if (!id && notify) {
9209 id = notify->uuid0;
9210 }
9211 if (id != 0) {
9212 pidNum = OSNumber::withNumber(id, 64);
9213 if (pidNum) {
9214 responseDescription->setObject(_statsPIDKey.get(), pidNum.get());
9215 }
9216 }
9217
9218 delayNum = OSNumber::withNumber(delay_ms, 32);
9219 if (delayNum) {
9220 responseDescription->setObject(_statsTimeMSKey.get(), delayNum.get());
9221 }
9222
9223 if (response->isEqualTo(gIOPMStatsDriverPSChangeSlow.get())) {
9224 powerCaps = OSNumber::withNumber(powerState, 32);
9225
9226 #if !defined(__i386__) && !defined(__x86_64__) && (DEVELOPMENT || DEBUG)
9227 static const char * driverCallTypes[] = {
9228 [kDriverCallInformPreChange] = "powerStateWillChangeTo",
9229 [kDriverCallInformPostChange] = "powerStateDidChangeTo",
9230 [kDriverCallSetPowerState] = "setPowerState"
9231 };
9232
9233 if (messageType < (sizeof(driverCallTypes) / sizeof(driverCallTypes[0]))) {
9234 DLOG("%s[0x%qx]::%s(%u) %stook %d ms\n",
9235 name, id, driverCallTypes[messageType], (uint32_t) powerState,
9236 async ? "async " : "", delay_ms);
9237 }
9238 #endif
9239 } else {
9240 powerCaps = OSNumber::withNumber(_pendingCapability, 32);
9241 }
9242 if (powerCaps) {
9243 responseDescription->setObject(_statsPowerCapsKey.get(), powerCaps.get());
9244 }
9245
9246 sleep = OSSymbol::withCString("Sleep");
9247 wake = OSSymbol::withCString("Wake");
9248 if (_systemTransitionType == kSystemTransitionSleep) {
9249 responseDescription->setObject(kIOPMStatsSystemTransitionKey, sleep.get());
9250 } else if (_systemTransitionType == kSystemTransitionWake) {
9251 responseDescription->setObject(kIOPMStatsSystemTransitionKey, wake.get());
9252 } else if (_systemTransitionType == kSystemTransitionCapability) {
9253 if (CAP_LOSS(kIOPMSystemCapabilityGraphics)) {
9254 responseDescription->setObject(kIOPMStatsSystemTransitionKey, sleep.get());
9255 } else if (CAP_GAIN(kIOPMSystemCapabilityGraphics)) {
9256 responseDescription->setObject(kIOPMStatsSystemTransitionKey, wake.get());
9257 }
9258 }
9259
9260 IOLockLock(pmStatsLock);
9261 if (pmStatsAppResponses && pmStatsAppResponses->getCount() < 50) {
9262 pmStatsAppResponses->setObject(responseDescription.get());
9263 }
9264 IOLockUnlock(pmStatsLock);
9265 }
9266
9267 return;
9268 }
9269
9270 // MARK: -
9271 // MARK: PMTraceWorker
9272
9273 //******************************************************************************
9274 // TracePoint support
9275 //
9276 //******************************************************************************
9277
9278 #define kIOPMRegisterNVRAMTracePointHandlerKey \
9279 "IOPMRegisterNVRAMTracePointHandler"
9280
9281 IOReturn
callPlatformFunction(const OSSymbol * functionName,bool waitForFunction,void * param1,void * param2,void * param3,void * param4)9282 IOPMrootDomain::callPlatformFunction(
9283 const OSSymbol * functionName,
9284 bool waitForFunction,
9285 void * param1, void * param2,
9286 void * param3, void * param4 )
9287 {
9288 if (pmTracer && functionName &&
9289 functionName->isEqualTo(kIOPMRegisterNVRAMTracePointHandlerKey) &&
9290 !pmTracer->tracePointHandler && !pmTracer->tracePointTarget) {
9291 uint32_t tracePointPhases, tracePointPCI;
9292 uint64_t statusCode;
9293
9294 pmTracer->tracePointHandler = (IOPMTracePointHandler) param1;
9295 pmTracer->tracePointTarget = (void *) param2;
9296 tracePointPCI = (uint32_t)(uintptr_t) param3;
9297 tracePointPhases = (uint32_t)(uintptr_t) param4;
9298 if ((tracePointPhases & 0xff) == kIOPMTracePointSystemSleep) {
9299 OSSharedPtr<IORegistryEntry> node = IORegistryEntry::fromPath( "/chosen", gIODTPlane );
9300 if (node) {
9301 OSSharedPtr<OSObject> bootRomFailureProp;
9302 bootRomFailureProp = node->copyProperty(kIOEFIBootRomFailureKey);
9303 OSData *data = OSDynamicCast(OSData, bootRomFailureProp.get());
9304 uint32_t bootFailureCode;
9305 if (data && data->getLength() == sizeof(bootFailureCode)) {
9306 // Failure code from EFI/BootRom is a four byte structure
9307 memcpy(&bootFailureCode, data->getBytesNoCopy(), sizeof(bootFailureCode));
9308 tracePointPCI = OSSwapBigToHostInt32(bootFailureCode);
9309 }
9310 }
9311 }
9312 statusCode = (((uint64_t)tracePointPCI) << 32) | tracePointPhases;
9313 if ((tracePointPhases & 0xff) != kIOPMTracePointSystemUp) {
9314 MSG("Sleep failure code 0x%08x 0x%08x\n",
9315 tracePointPCI, tracePointPhases);
9316 }
9317 setProperty(kIOPMSleepWakeFailureCodeKey, statusCode, 64);
9318 pmTracer->tracePointHandler( pmTracer->tracePointTarget, 0, 0 );
9319
9320 return kIOReturnSuccess;
9321 }
9322 #if HIBERNATION
9323 else if (functionName &&
9324 functionName->isEqualTo(kIOPMInstallSystemSleepPolicyHandlerKey)) {
9325 if (gSleepPolicyHandler) {
9326 return kIOReturnExclusiveAccess;
9327 }
9328 if (!param1) {
9329 return kIOReturnBadArgument;
9330 }
9331 gSleepPolicyHandler = (IOPMSystemSleepPolicyHandler) param1;
9332 gSleepPolicyTarget = (void *) param2;
9333 setProperty("IOPMSystemSleepPolicyHandler", kOSBooleanTrue);
9334 return kIOReturnSuccess;
9335 }
9336 #endif
9337
9338 return super::callPlatformFunction(
9339 functionName, waitForFunction, param1, param2, param3, param4);
9340 }
9341
9342 void
kdebugTrace(uint32_t event,uint64_t id,uintptr_t param1,uintptr_t param2,uintptr_t param3)9343 IOPMrootDomain::kdebugTrace(uint32_t event, uint64_t id,
9344 uintptr_t param1, uintptr_t param2, uintptr_t param3)
9345 {
9346 uint32_t code = IODBG_POWER(event);
9347 uint64_t regId = id;
9348 if (regId == 0) {
9349 regId = getRegistryEntryID();
9350 }
9351 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, code, (uintptr_t) regId, param1, param2, param3, 0);
9352 }
9353
9354 void
tracePoint(uint8_t point)9355 IOPMrootDomain::tracePoint( uint8_t point )
9356 {
9357 if (systemBooting) {
9358 return;
9359 }
9360
9361 if (kIOPMTracePointWakeCapabilityClients == point) {
9362 acceptSystemWakeEvents(kAcceptSystemWakeEvents_Disable);
9363 }
9364
9365 kdebugTrace(kPMLogSleepWakeTracePoint, 0, point, 0);
9366 pmTracer->tracePoint(point);
9367 }
9368
9369 static void
kext_log_putc(char c)9370 kext_log_putc(char c)
9371 {
9372 if (gKextNameEnd || gKextNamePos >= (sizeof(gKextNameBuf) - 1)) {
9373 return;
9374 }
9375 if (c == '(' || c == '[' || c == ' ') {
9376 c = 0;
9377 gKextNameEnd = true;
9378 }
9379
9380 gKextNameBuf[gKextNamePos++] = c;
9381 }
9382
9383 static int
kext_log(const char * fmt,...)9384 kext_log(const char *fmt, ...)
9385 {
9386 va_list listp;
9387
9388 va_start(listp, fmt);
9389 _doprnt(fmt, &listp, &kext_log_putc, 16);
9390 va_end(listp);
9391
9392 return 0;
9393 }
9394
9395 static OSPtr<const OSSymbol>
copyKextIdentifierWithAddress(vm_address_t address)9396 copyKextIdentifierWithAddress(vm_address_t address)
9397 {
9398 OSSharedPtr<const OSSymbol> identifer;
9399
9400 IOLockLock(gHaltLogLock);
9401
9402 gKextNameEnd = false;
9403 gKextNamePos = 0;
9404 gKextNameBuf[0] = 0;
9405
9406 OSKext::printKextsInBacktrace(&address, 1, kext_log, OSKext::kPrintKextsLock | OSKext::kPrintKextsTerse);
9407 gKextNameBuf[sizeof(gKextNameBuf) - 1] = 0;
9408 identifer = OSSymbol::withCString((gKextNameBuf[0] != 0) ? gKextNameBuf : kOSKextKernelIdentifier);
9409
9410 IOLockUnlock(gHaltLogLock);
9411
9412 return identifer;
9413 }
9414
9415 // Caller serialized using PM workloop
9416 const char *
getNotificationClientName(OSObject * object)9417 IOPMrootDomain::getNotificationClientName(OSObject *object)
9418 {
9419 IOPMServiceInterestNotifier *notifier = (typeof(notifier))object;
9420 const char *clientName = "UNKNOWN";
9421
9422 if (!notifier->clientName) {
9423 // Check for user client
9424 if (systemCapabilityNotifier && (((IOPMServiceInterestNotifier *) systemCapabilityNotifier.get())->handler == notifier->handler)) {
9425 OSNumber *clientID = NULL;
9426 messageClient(kIOMessageCopyClientID, object, &clientID);
9427 if (clientID) {
9428 OSSharedPtr<OSString> string(IOCopyLogNameForPID(clientID->unsigned32BitValue()), OSNoRetain);
9429 if (string) {
9430 notifier->clientName = OSSymbol::withString(string.get());
9431 }
9432 clientID->release();
9433 }
9434 } else if (notifier->identifier) {
9435 notifier->clientName.reset(notifier->identifier.get(), OSRetain);
9436 }
9437 }
9438
9439 if (notifier->clientName) {
9440 clientName = notifier->clientName->getCStringNoCopy();
9441 }
9442
9443 return clientName;
9444 }
9445
9446 void
traceNotification(OSObject * object,bool start,uint64_t timestamp,uint32_t msgIndex)9447 IOPMrootDomain::traceNotification(OSObject *object, bool start, uint64_t timestamp, uint32_t msgIndex)
9448 {
9449 IOPMServiceInterestNotifier *notifier;
9450
9451 if (systemBooting) {
9452 return;
9453 }
9454 notifier = OSDynamicCast(IOPMServiceInterestNotifier, object);
9455 if (!notifier) {
9456 return;
9457 }
9458
9459 if (start) {
9460 pmTracer->traceDetail(notifier->uuid0 >> 32);
9461 kdebugTrace(kPMLogSleepWakeMessage, pmTracer->getTracePhase(),
9462 (uintptr_t) notifier->msgType, (uintptr_t) notifier->uuid0, (uintptr_t) notifier->uuid1);
9463
9464 // Update notifier state used for response/ack logging
9465 notifier->msgIndex = msgIndex;
9466 notifier->msgAbsTime = timestamp;
9467
9468 if (msgIndex != UINT_MAX) {
9469 DLOG("%s[%u] to %s\n", getIOMessageString(notifier->msgType), msgIndex, getNotificationClientName(notifier));
9470 } else {
9471 DLOG("%s to %s\n", getIOMessageString(notifier->msgType), getNotificationClientName(notifier));
9472 }
9473
9474 assert(notifierObject == NULL);
9475 notifierThread = current_thread();
9476 notifierObject.reset(notifier, OSRetain);
9477 } else {
9478 uint64_t nsec;
9479 uint32_t delayMS;
9480
9481 SUB_ABSOLUTETIME(×tamp, ¬ifier->msgAbsTime);
9482 absolutetime_to_nanoseconds(timestamp, &nsec);
9483 delayMS = (uint32_t)(nsec / 1000000ULL);
9484 if (delayMS > notifier->maxMsgDelayMS) {
9485 notifier->maxMsgDelayMS = delayMS;
9486 }
9487
9488 assert(notifierObject == notifier);
9489 notifierObject.reset();
9490 notifierThread = NULL;
9491 }
9492 }
9493
9494 void
traceNotificationAck(OSObject * object,uint32_t delay_ms)9495 IOPMrootDomain::traceNotificationAck(OSObject *object, uint32_t delay_ms)
9496 {
9497 if (systemBooting) {
9498 return;
9499 }
9500 IOPMServiceInterestNotifier *notifier = OSDynamicCast(IOPMServiceInterestNotifier, object);
9501 if (!notifier) {
9502 return;
9503 }
9504
9505 kdebugTrace(kPMLogDrvResponseDelay, notifier->uuid0,
9506 (uintptr_t) notifier->uuid1, (uintptr_t) 0, (uintptr_t) delay_ms);
9507
9508 DLOG("%s[%u] ack from %s took %d ms\n",
9509 getIOMessageString(notifier->msgType), notifier->msgIndex, getNotificationClientName(notifier), delay_ms);
9510 if (delay_ms > notifier->maxAckDelayMS) {
9511 notifier->maxAckDelayMS = delay_ms;
9512 }
9513 }
9514
9515 void
traceNotificationResponse(OSObject * object,uint32_t delay_ms,uint32_t ack_time_us)9516 IOPMrootDomain::traceNotificationResponse(OSObject *object, uint32_t delay_ms, uint32_t ack_time_us)
9517 {
9518 if (systemBooting) {
9519 return;
9520 }
9521 IOPMServiceInterestNotifier *notifier = OSDynamicCast(IOPMServiceInterestNotifier, object);
9522 if (!notifier) {
9523 return;
9524 }
9525
9526 kdebugTrace(kPMLogDrvResponseDelay, notifier->uuid0,
9527 (uintptr_t) notifier->uuid1, (uintptr_t)(ack_time_us / 1000), (uintptr_t) delay_ms);
9528
9529 if (ack_time_us == 0) {
9530 // Client work is done and ack will not be forthcoming
9531 DLOG("%s[%u] response from %s took %d ms\n",
9532 getIOMessageString(notifier->msgType), notifier->msgIndex, getNotificationClientName(notifier), delay_ms);
9533 } else {
9534 // Client needs more time and it must ack within ack_time_us
9535 DLOG("%s[%u] response from %s took %d ms (ack in %d us)\n",
9536 getIOMessageString(notifier->msgType), notifier->msgIndex, getNotificationClientName(notifier), delay_ms, ack_time_us);
9537 }
9538 }
9539
9540 void
traceFilteredNotification(OSObject * object)9541 IOPMrootDomain::traceFilteredNotification(OSObject *object)
9542 {
9543 if ((kIOLogDebugPower & gIOKitDebug) == 0) {
9544 return;
9545 }
9546 if (systemBooting) {
9547 return;
9548 }
9549 IOPMServiceInterestNotifier *notifier = OSDynamicCast(IOPMServiceInterestNotifier, object);
9550 if (!notifier) {
9551 return;
9552 }
9553
9554 DLOG("%s to %s dropped\n", getIOMessageString(notifier->msgType), getNotificationClientName(notifier));
9555 }
9556
9557 void
traceDetail(uint32_t msgType,uint32_t msgIndex,uint32_t delay)9558 IOPMrootDomain::traceDetail(uint32_t msgType, uint32_t msgIndex, uint32_t delay)
9559 {
9560 if (!systemBooting) {
9561 uint32_t detail = ((msgType & 0xffff) << 16) | (delay & 0xffff);
9562 pmTracer->traceDetail( detail );
9563 kdebugTrace(kPMLogSleepWakeTracePoint, pmTracer->getTracePhase(), msgType, delay);
9564 DLOG("trace point 0x%02x msgType 0x%x detail 0x%08x\n", pmTracer->getTracePhase(), msgType, delay);
9565 }
9566 }
9567
9568 void
configureReportGated(uint64_t channel_id,uint64_t action,void * result)9569 IOPMrootDomain::configureReportGated(uint64_t channel_id, uint64_t action, void *result)
9570 {
9571 size_t reportSize;
9572 void **report = NULL;
9573 uint32_t bktCnt;
9574 uint32_t bktSize;
9575 uint32_t *clientCnt;
9576
9577 ASSERT_GATED();
9578
9579 report = NULL;
9580 if (channel_id == kAssertDelayChID) {
9581 report = &assertOnWakeReport;
9582 bktCnt = kAssertDelayBcktCnt;
9583 bktSize = kAssertDelayBcktSize;
9584 clientCnt = &assertOnWakeClientCnt;
9585 } else if (channel_id == kSleepDelaysChID) {
9586 report = &sleepDelaysReport;
9587 bktCnt = kSleepDelaysBcktCnt;
9588 bktSize = kSleepDelaysBcktSize;
9589 clientCnt = &sleepDelaysClientCnt;
9590 } else {
9591 assert(false);
9592 return;
9593 }
9594
9595 switch (action) {
9596 case kIOReportEnable:
9597
9598 if (*report) {
9599 (*clientCnt)++;
9600 break;
9601 }
9602
9603 reportSize = HISTREPORT_BUFSIZE(bktCnt);
9604 *report = IOMallocZeroData(reportSize);
9605 if (*report == NULL) {
9606 break;
9607 }
9608 HISTREPORT_INIT((uint16_t)bktCnt, bktSize, *report, reportSize,
9609 getRegistryEntryID(), channel_id, kIOReportCategoryPower);
9610
9611 if (channel_id == kAssertDelayChID) {
9612 assertOnWakeSecs = 0;
9613 }
9614
9615 break;
9616
9617 case kIOReportDisable:
9618 if (*clientCnt == 0) {
9619 break;
9620 }
9621 if (*clientCnt == 1) {
9622 IOFreeData(*report, HISTREPORT_BUFSIZE(bktCnt));
9623 *report = NULL;
9624 }
9625 (*clientCnt)--;
9626
9627 if (channel_id == kAssertDelayChID) {
9628 assertOnWakeSecs = -1; // Invalid value to prevent updates
9629 }
9630 break;
9631
9632 case kIOReportGetDimensions:
9633 if (*report) {
9634 HISTREPORT_UPDATERES(*report, kIOReportGetDimensions, result);
9635 }
9636 break;
9637 }
9638
9639 return;
9640 }
9641
9642 IOReturn
configureReport(IOReportChannelList * channelList,IOReportConfigureAction action,void * result,void * destination)9643 IOPMrootDomain::configureReport(IOReportChannelList *channelList,
9644 IOReportConfigureAction action,
9645 void *result,
9646 void *destination)
9647 {
9648 unsigned cnt;
9649 uint64_t configAction = (uint64_t)action;
9650
9651 for (cnt = 0; cnt < channelList->nchannels; cnt++) {
9652 if ((channelList->channels[cnt].channel_id == kSleepCntChID) ||
9653 (channelList->channels[cnt].channel_id == kDarkWkCntChID) ||
9654 (channelList->channels[cnt].channel_id == kUserWkCntChID)) {
9655 if (action != kIOReportGetDimensions) {
9656 continue;
9657 }
9658 SIMPLEREPORT_UPDATERES(kIOReportGetDimensions, result);
9659 } else if ((channelList->channels[cnt].channel_id == kAssertDelayChID) ||
9660 (channelList->channels[cnt].channel_id == kSleepDelaysChID)) {
9661 gIOPMWorkLoop->runAction(
9662 OSMemberFunctionCast(IOWorkLoop::Action, this, &IOPMrootDomain::configureReportGated),
9663 (OSObject *)this, (void *)channelList->channels[cnt].channel_id,
9664 (void *)configAction, (void *)result);
9665 }
9666 }
9667
9668 return super::configureReport(channelList, action, result, destination);
9669 }
9670
9671 IOReturn
updateReportGated(uint64_t ch_id,void * result,IOBufferMemoryDescriptor * dest)9672 IOPMrootDomain::updateReportGated(uint64_t ch_id, void *result, IOBufferMemoryDescriptor *dest)
9673 {
9674 uint32_t size2cpy;
9675 void *data2cpy;
9676 void **report;
9677
9678 ASSERT_GATED();
9679
9680 report = NULL;
9681 if (ch_id == kAssertDelayChID) {
9682 report = &assertOnWakeReport;
9683 } else if (ch_id == kSleepDelaysChID) {
9684 report = &sleepDelaysReport;
9685 } else {
9686 assert(false);
9687 return kIOReturnBadArgument;
9688 }
9689
9690 if (*report == NULL) {
9691 return kIOReturnNotOpen;
9692 }
9693
9694 HISTREPORT_UPDATEPREP(*report, data2cpy, size2cpy);
9695 if (size2cpy > (dest->getCapacity() - dest->getLength())) {
9696 return kIOReturnOverrun;
9697 }
9698
9699 HISTREPORT_UPDATERES(*report, kIOReportCopyChannelData, result);
9700 dest->appendBytes(data2cpy, size2cpy);
9701
9702 return kIOReturnSuccess;
9703 }
9704
9705 IOReturn
updateReport(IOReportChannelList * channelList,IOReportUpdateAction action,void * result,void * destination)9706 IOPMrootDomain::updateReport(IOReportChannelList *channelList,
9707 IOReportUpdateAction action,
9708 void *result,
9709 void *destination)
9710 {
9711 uint32_t size2cpy;
9712 void *data2cpy;
9713 uint8_t buf[SIMPLEREPORT_BUFSIZE];
9714 IOBufferMemoryDescriptor *dest = OSDynamicCast(IOBufferMemoryDescriptor, (OSObject *)destination);
9715 unsigned cnt;
9716 uint64_t ch_id;
9717
9718 if (action != kIOReportCopyChannelData) {
9719 goto exit;
9720 }
9721
9722 for (cnt = 0; cnt < channelList->nchannels; cnt++) {
9723 ch_id = channelList->channels[cnt].channel_id;
9724
9725 if ((ch_id == kAssertDelayChID) || (ch_id == kSleepDelaysChID)) {
9726 gIOPMWorkLoop->runAction(
9727 OSMemberFunctionCast(IOWorkLoop::Action, this, &IOPMrootDomain::updateReportGated),
9728 (OSObject *)this, (void *)ch_id,
9729 (void *)result, (void *)dest);
9730 continue;
9731 } else if ((ch_id == kSleepCntChID) ||
9732 (ch_id == kDarkWkCntChID) || (ch_id == kUserWkCntChID)) {
9733 SIMPLEREPORT_INIT(buf, sizeof(buf), getRegistryEntryID(), ch_id, kIOReportCategoryPower);
9734 } else {
9735 continue;
9736 }
9737
9738 if (ch_id == kSleepCntChID) {
9739 SIMPLEREPORT_SETVALUE(buf, sleepCnt);
9740 } else if (ch_id == kDarkWkCntChID) {
9741 SIMPLEREPORT_SETVALUE(buf, darkWakeCnt);
9742 } else if (ch_id == kUserWkCntChID) {
9743 SIMPLEREPORT_SETVALUE(buf, displayWakeCnt);
9744 }
9745
9746 SIMPLEREPORT_UPDATEPREP(buf, data2cpy, size2cpy);
9747 SIMPLEREPORT_UPDATERES(kIOReportCopyChannelData, result);
9748 dest->appendBytes(data2cpy, size2cpy);
9749 }
9750
9751 exit:
9752 return super::updateReport(channelList, action, result, destination);
9753 }
9754
9755
9756 //******************************************************************************
9757 // PMTraceWorker Class
9758 //
9759 //******************************************************************************
9760
9761 #undef super
9762 #define super OSObject
OSDefineMetaClassAndStructors(PMTraceWorker,OSObject)9763 OSDefineMetaClassAndStructors(PMTraceWorker, OSObject)
9764
9765 #define kPMBestGuessPCIDevicesCount 25
9766 #define kPMMaxRTCBitfieldSize 32
9767
9768 OSPtr<PMTraceWorker>
9769 PMTraceWorker::tracer(IOPMrootDomain * owner)
9770 {
9771 OSSharedPtr<PMTraceWorker> me = OSMakeShared<PMTraceWorker>();
9772 if (!me || !me->init()) {
9773 return NULL;
9774 }
9775
9776 DLOG("PMTraceWorker %p\n", OBFUSCATE(me.get()));
9777
9778 // Note that we cannot instantiate the PCI device -> bit mappings here, since
9779 // the IODeviceTree has not yet been created by IOPlatformExpert. We create
9780 // this dictionary lazily.
9781 me->owner = owner;
9782 me->pciDeviceBitMappings = NULL;
9783 me->pmTraceWorkerLock = IOLockAlloc();
9784 me->tracePhase = kIOPMTracePointSystemUp;
9785 me->traceData32 = 0;
9786 me->loginWindowData = 0;
9787 me->coreDisplayData = 0;
9788 me->coreGraphicsData = 0;
9789 return me;
9790 }
9791
9792 void
RTC_TRACE(void)9793 PMTraceWorker::RTC_TRACE(void)
9794 {
9795 if (tracePointHandler && tracePointTarget) {
9796 uint32_t wordA;
9797
9798 IOLockLock(pmTraceWorkerLock);
9799 wordA = (loginWindowData << 24) | (coreDisplayData << 16) |
9800 (coreGraphicsData << 8) | tracePhase;
9801 IOLockUnlock(pmTraceWorkerLock);
9802
9803 tracePointHandler( tracePointTarget, traceData32, wordA );
9804 _LOG("RTC_TRACE wrote 0x%08x 0x%08x\n", traceData32, wordA);
9805 }
9806 #if DEVELOPMENT || DEBUG
9807 if ((swd_panic_phase != 0) && (swd_panic_phase == tracePhase)) {
9808 DEBUG_LOG("Causing sleep wake failure in phase 0x%08x\n", tracePhase);
9809 IOLock *l = IOLockAlloc();
9810 IOLockLock(l);
9811 IOLockLock(l);
9812 }
9813 #endif
9814 }
9815
9816 int
recordTopLevelPCIDevice(IOService * pciDevice)9817 PMTraceWorker::recordTopLevelPCIDevice(IOService * pciDevice)
9818 {
9819 OSSharedPtr<const OSSymbol> deviceName;
9820 int index = -1;
9821
9822 IOLockLock(pmTraceWorkerLock);
9823
9824 if (!pciDeviceBitMappings) {
9825 pciDeviceBitMappings = OSArray::withCapacity(kPMBestGuessPCIDevicesCount);
9826 if (!pciDeviceBitMappings) {
9827 goto exit;
9828 }
9829 }
9830
9831 // Check for bitmask overflow.
9832 if (pciDeviceBitMappings->getCount() >= kPMMaxRTCBitfieldSize) {
9833 goto exit;
9834 }
9835
9836 if ((deviceName = pciDevice->copyName()) &&
9837 (pciDeviceBitMappings->getNextIndexOfObject(deviceName.get(), 0) == (unsigned int)-1) &&
9838 pciDeviceBitMappings->setObject(deviceName.get())) {
9839 index = pciDeviceBitMappings->getCount() - 1;
9840 _LOG("PMTrace PCI array: set object %s => %d\n",
9841 deviceName->getCStringNoCopy(), index);
9842 }
9843
9844 if (!addedToRegistry && (index >= 0)) {
9845 addedToRegistry = owner->setProperty("PCITopLevel", this);
9846 }
9847
9848 exit:
9849 IOLockUnlock(pmTraceWorkerLock);
9850 return index;
9851 }
9852
9853 bool
serialize(OSSerialize * s) const9854 PMTraceWorker::serialize(OSSerialize *s) const
9855 {
9856 bool ok = false;
9857 if (pciDeviceBitMappings) {
9858 IOLockLock(pmTraceWorkerLock);
9859 ok = pciDeviceBitMappings->serialize(s);
9860 IOLockUnlock(pmTraceWorkerLock);
9861 }
9862 return ok;
9863 }
9864
9865 void
tracePoint(uint8_t phase)9866 PMTraceWorker::tracePoint(uint8_t phase)
9867 {
9868 // clear trace detail when phase begins
9869 if (tracePhase != phase) {
9870 traceData32 = 0;
9871 }
9872
9873 tracePhase = phase;
9874
9875 DLOG("trace point 0x%02x\n", tracePhase);
9876 RTC_TRACE();
9877 }
9878
9879 void
traceDetail(uint32_t detail)9880 PMTraceWorker::traceDetail(uint32_t detail)
9881 {
9882 if (detail == traceData32) {
9883 return;
9884 }
9885 traceData32 = detail;
9886 RTC_TRACE();
9887 }
9888
9889 void
traceComponentWakeProgress(uint32_t component,uint32_t data)9890 PMTraceWorker::traceComponentWakeProgress(uint32_t component, uint32_t data)
9891 {
9892 switch (component) {
9893 case kIOPMLoginWindowProgress:
9894 loginWindowData = data & kIOPMLoginWindowProgressMask;
9895 break;
9896 case kIOPMCoreDisplayProgress:
9897 coreDisplayData = data & kIOPMCoreDisplayProgressMask;
9898 break;
9899 case kIOPMCoreGraphicsProgress:
9900 coreGraphicsData = data & kIOPMCoreGraphicsProgressMask;
9901 break;
9902 default:
9903 return;
9904 }
9905
9906 DLOG("component trace point 0x%02x data 0x%08x\n", component, data);
9907 RTC_TRACE();
9908 }
9909
9910 void
tracePCIPowerChange(change_t type,IOService * service,uint32_t changeFlags,uint32_t bitNum)9911 PMTraceWorker::tracePCIPowerChange(
9912 change_t type, IOService *service, uint32_t changeFlags, uint32_t bitNum)
9913 {
9914 uint32_t bitMask;
9915 uint32_t expectedFlag;
9916
9917 // Ignore PCI changes outside of system sleep/wake.
9918 if ((kIOPMTracePointSleepPowerPlaneDrivers != tracePhase) &&
9919 (kIOPMTracePointWakePowerPlaneDrivers != tracePhase)) {
9920 return;
9921 }
9922
9923 // Only record the WillChange transition when going to sleep,
9924 // and the DidChange on the way up.
9925 changeFlags &= (kIOPMDomainWillChange | kIOPMDomainDidChange);
9926 expectedFlag = (kIOPMTracePointSleepPowerPlaneDrivers == tracePhase) ?
9927 kIOPMDomainWillChange : kIOPMDomainDidChange;
9928 if (changeFlags != expectedFlag) {
9929 return;
9930 }
9931
9932 // Mark this device off in our bitfield
9933 if (bitNum < kPMMaxRTCBitfieldSize) {
9934 bitMask = (1 << bitNum);
9935
9936 if (kPowerChangeStart == type) {
9937 traceData32 |= bitMask;
9938 _LOG("PMTrace: Device %s started - bit %2d mask 0x%08x => 0x%08x\n",
9939 service->getName(), bitNum, bitMask, traceData32);
9940 owner->kdebugTrace(kPMLogPCIDevChangeStart, service->getRegistryEntryID(), traceData32, 0);
9941 } else {
9942 traceData32 &= ~bitMask;
9943 _LOG("PMTrace: Device %s finished - bit %2d mask 0x%08x => 0x%08x\n",
9944 service->getName(), bitNum, bitMask, traceData32);
9945 owner->kdebugTrace(kPMLogPCIDevChangeDone, service->getRegistryEntryID(), traceData32, 0);
9946 }
9947
9948 DLOG("trace point 0x%02x detail 0x%08x\n", tracePhase, traceData32);
9949 RTC_TRACE();
9950 }
9951 }
9952
9953 uint64_t
getPMStatusCode()9954 PMTraceWorker::getPMStatusCode()
9955 {
9956 return ((uint64_t)traceData32 << 32) | ((uint64_t)tracePhase);
9957 }
9958
9959 uint8_t
getTracePhase()9960 PMTraceWorker::getTracePhase()
9961 {
9962 return tracePhase;
9963 }
9964
9965 uint32_t
getTraceData()9966 PMTraceWorker::getTraceData()
9967 {
9968 return traceData32;
9969 }
9970
9971 // MARK: -
9972 // MARK: PMHaltWorker
9973
9974 //******************************************************************************
9975 // PMHaltWorker Class
9976 //
9977 //******************************************************************************
9978
9979 PMHaltWorker *
worker(void)9980 PMHaltWorker::worker( void )
9981 {
9982 PMHaltWorker * me;
9983 IOThread thread;
9984
9985 do {
9986 me = OSTypeAlloc( PMHaltWorker );
9987 if (!me || !me->init()) {
9988 break;
9989 }
9990
9991 me->lock = IOLockAlloc();
9992 if (!me->lock) {
9993 break;
9994 }
9995
9996 DLOG("PMHaltWorker %p\n", OBFUSCATE(me));
9997 me->retain(); // thread holds extra retain
9998 if (KERN_SUCCESS != kernel_thread_start(&PMHaltWorker::main, (void *) me, &thread)) {
9999 me->release();
10000 break;
10001 }
10002 thread_deallocate(thread);
10003 return me;
10004 } while (false);
10005
10006 if (me) {
10007 me->release();
10008 }
10009 return NULL;
10010 }
10011
10012 void
free(void)10013 PMHaltWorker::free( void )
10014 {
10015 DLOG("PMHaltWorker free %p\n", OBFUSCATE(this));
10016 if (lock) {
10017 IOLockFree(lock);
10018 lock = NULL;
10019 }
10020 return OSObject::free();
10021 }
10022
10023 void
main(void * arg,wait_result_t waitResult)10024 PMHaltWorker::main( void * arg, wait_result_t waitResult )
10025 {
10026 PMHaltWorker * me = (PMHaltWorker *) arg;
10027
10028 IOLockLock( gPMHaltLock );
10029 gPMHaltBusyCount++;
10030 me->depth = gPMHaltDepth;
10031 IOLockUnlock( gPMHaltLock );
10032
10033 while (me->depth >= 0) {
10034 PMHaltWorker::work( me );
10035
10036 IOLockLock( gPMHaltLock );
10037 if (++gPMHaltIdleCount >= gPMHaltBusyCount) {
10038 // This is the last thread to finish work on this level,
10039 // inform everyone to start working on next lower level.
10040 gPMHaltDepth--;
10041 me->depth = gPMHaltDepth;
10042 gPMHaltIdleCount = 0;
10043 thread_wakeup((event_t) &gPMHaltIdleCount);
10044 } else {
10045 // One or more threads are still working on this level,
10046 // this thread must wait.
10047 me->depth = gPMHaltDepth - 1;
10048 do {
10049 IOLockSleep(gPMHaltLock, &gPMHaltIdleCount, THREAD_UNINT);
10050 } while (me->depth != gPMHaltDepth);
10051 }
10052 IOLockUnlock( gPMHaltLock );
10053 }
10054
10055 // No more work to do, terminate thread
10056 DLOG("All done for worker: %p (visits = %u)\n", OBFUSCATE(me), me->visits);
10057 thread_wakeup( &gPMHaltDepth );
10058 me->release();
10059 }
10060
10061 void
work(PMHaltWorker * me)10062 PMHaltWorker::work( PMHaltWorker * me )
10063 {
10064 OSSharedPtr<IOService> service;
10065 OSSet * inner;
10066 AbsoluteTime startTime, elapsedTime;
10067 UInt32 deltaTime;
10068 bool timeout;
10069
10070 while (true) {
10071 timeout = false;
10072
10073 // Claim an unit of work from the shared pool
10074 IOLockLock( gPMHaltLock );
10075 inner = (OSSet *)gPMHaltArray->getObject(me->depth);
10076 if (inner) {
10077 service.reset(OSDynamicCast(IOService, inner->getAnyObject()), OSRetain);
10078 if (service) {
10079 inner->removeObject(service.get());
10080 }
10081 }
10082 IOLockUnlock( gPMHaltLock );
10083 if (!service) {
10084 break; // no more work at this depth
10085 }
10086 clock_get_uptime(&startTime);
10087
10088 if (!service->isInactive() &&
10089 service->setProperty(gPMHaltClientAcknowledgeKey.get(), me)) {
10090 IOLockLock(me->lock);
10091 me->startTime = startTime;
10092 me->service = service.get();
10093 me->timeout = false;
10094 IOLockUnlock(me->lock);
10095
10096 service->systemWillShutdown( gPMHaltMessageType);
10097
10098 // Wait for driver acknowledgement
10099 IOLockLock(me->lock);
10100 while (service->propertyExists(gPMHaltClientAcknowledgeKey.get())) {
10101 IOLockSleep(me->lock, me, THREAD_UNINT);
10102 }
10103 me->service = NULL;
10104 timeout = me->timeout;
10105 IOLockUnlock(me->lock);
10106 }
10107
10108 deltaTime = computeDeltaTimeMS(&startTime, &elapsedTime);
10109 if ((deltaTime > kPMHaltTimeoutMS) || timeout) {
10110 LOG("%s driver %s (0x%llx) took %u ms\n",
10111 (gPMHaltMessageType == kIOMessageSystemWillPowerOff) ?
10112 "PowerOff" : "Restart",
10113 service->getName(), service->getRegistryEntryID(),
10114 (uint32_t) deltaTime );
10115 halt_log_enter("PowerOff/Restart handler completed",
10116 OSMemberFunctionCast(const void *, service.get(), &IOService::systemWillShutdown),
10117 elapsedTime);
10118 }
10119
10120 me->visits++;
10121 }
10122 }
10123
10124 void
checkTimeout(PMHaltWorker * me,AbsoluteTime * now)10125 PMHaltWorker::checkTimeout( PMHaltWorker * me, AbsoluteTime * now )
10126 {
10127 UInt64 nano;
10128 AbsoluteTime startTime;
10129 AbsoluteTime endTime;
10130
10131 endTime = *now;
10132
10133 IOLockLock(me->lock);
10134 if (me->service && !me->timeout) {
10135 startTime = me->startTime;
10136 nano = 0;
10137 if (CMP_ABSOLUTETIME(&endTime, &startTime) > 0) {
10138 SUB_ABSOLUTETIME(&endTime, &startTime);
10139 absolutetime_to_nanoseconds(endTime, &nano);
10140 }
10141 if (nano > 3000000000ULL) {
10142 me->timeout = true;
10143
10144 halt_log_enter("PowerOff/Restart still waiting on handler",
10145 OSMemberFunctionCast(const void *, me->service, &IOService::systemWillShutdown),
10146 endTime);
10147 MSG("%s still waiting on %s\n",
10148 (gPMHaltMessageType == kIOMessageSystemWillPowerOff) ? "PowerOff" : "Restart",
10149 me->service->getName());
10150 }
10151 }
10152 IOLockUnlock(me->lock);
10153 }
10154
10155 //******************************************************************************
10156 // acknowledgeSystemWillShutdown
10157 //
10158 // Acknowledgement from drivers that they have prepared for shutdown/restart.
10159 //******************************************************************************
10160
10161 void
acknowledgeSystemWillShutdown(IOService * from)10162 IOPMrootDomain::acknowledgeSystemWillShutdown( IOService * from )
10163 {
10164 PMHaltWorker * worker;
10165 OSSharedPtr<OSObject> prop;
10166
10167 if (!from) {
10168 return;
10169 }
10170
10171 //DLOG("%s acknowledged\n", from->getName());
10172 prop = from->copyProperty( gPMHaltClientAcknowledgeKey.get());
10173 if (prop) {
10174 worker = (PMHaltWorker *) prop.get();
10175 IOLockLock(worker->lock);
10176 from->removeProperty( gPMHaltClientAcknowledgeKey.get());
10177 thread_wakeup((event_t) worker);
10178 IOLockUnlock(worker->lock);
10179 } else {
10180 DLOG("%s acknowledged without worker property\n",
10181 from->getName());
10182 }
10183 }
10184
10185
10186 //******************************************************************************
10187 // notifySystemShutdown
10188 //
10189 // Notify all objects in PM tree that system will shutdown or restart
10190 //******************************************************************************
10191
10192 static void
notifySystemShutdown(IOService * root,uint32_t messageType)10193 notifySystemShutdown( IOService * root, uint32_t messageType )
10194 {
10195 #define PLACEHOLDER ((OSSet *)gPMHaltArray.get())
10196 OSSharedPtr<IORegistryIterator> iter;
10197 IORegistryEntry * entry;
10198 IOService * node;
10199 OSSet * inner;
10200 OSSharedPtr<OSSet> newInner;
10201 PMHaltWorker * workers[kPMHaltMaxWorkers];
10202 AbsoluteTime deadline;
10203 unsigned int totalNodes = 0;
10204 unsigned int depth;
10205 unsigned int rootDepth;
10206 unsigned int numWorkers;
10207 unsigned int count;
10208 int waitResult;
10209 void * baseFunc;
10210 bool ok;
10211
10212 DLOG("%s msgType = 0x%x\n", __FUNCTION__, messageType);
10213
10214 baseFunc = OSMemberFunctionCast(void *, root, &IOService::systemWillShutdown);
10215
10216 // Iterate the entire PM tree starting from root
10217
10218 rootDepth = root->getDepth( gIOPowerPlane );
10219 if (!rootDepth) {
10220 goto done;
10221 }
10222
10223 // debug - for repeated test runs
10224 while (PMHaltWorker::metaClass->getInstanceCount()) {
10225 IOSleep(1);
10226 }
10227
10228 if (!gPMHaltArray) {
10229 gPMHaltArray = OSArray::withCapacity(40);
10230 if (!gPMHaltArray) {
10231 goto done;
10232 }
10233 } else { // debug
10234 gPMHaltArray->flushCollection();
10235 }
10236
10237 if (!gPMHaltLock) {
10238 gPMHaltLock = IOLockAlloc();
10239 if (!gPMHaltLock) {
10240 goto done;
10241 }
10242 }
10243
10244 if (!gPMHaltClientAcknowledgeKey) {
10245 gPMHaltClientAcknowledgeKey =
10246 OSSymbol::withCStringNoCopy("PMShutdown");
10247 if (!gPMHaltClientAcknowledgeKey) {
10248 goto done;
10249 }
10250 }
10251
10252 gPMHaltMessageType = messageType;
10253
10254 // Depth-first walk of PM plane
10255
10256 iter = IORegistryIterator::iterateOver(
10257 root, gIOPowerPlane, kIORegistryIterateRecursively);
10258
10259 if (iter) {
10260 while ((entry = iter->getNextObject())) {
10261 node = OSDynamicCast(IOService, entry);
10262 if (!node) {
10263 continue;
10264 }
10265
10266 if (baseFunc ==
10267 OSMemberFunctionCast(void *, node, &IOService::systemWillShutdown)) {
10268 continue;
10269 }
10270
10271 depth = node->getDepth( gIOPowerPlane );
10272 if (depth <= rootDepth) {
10273 continue;
10274 }
10275
10276 ok = false;
10277
10278 // adjust to zero based depth
10279 depth -= (rootDepth + 1);
10280
10281 // gPMHaltArray is an array of containers, each container
10282 // refers to nodes with the same depth.
10283
10284 count = gPMHaltArray->getCount();
10285 while (depth >= count) {
10286 // expand array and insert placeholders
10287 gPMHaltArray->setObject(PLACEHOLDER);
10288 count++;
10289 }
10290 count = gPMHaltArray->getCount();
10291 if (depth < count) {
10292 inner = (OSSet *)gPMHaltArray->getObject(depth);
10293 if (inner == PLACEHOLDER) {
10294 newInner = OSSet::withCapacity(40);
10295 if (newInner) {
10296 gPMHaltArray->replaceObject(depth, newInner.get());
10297 inner = newInner.get();
10298 }
10299 }
10300
10301 // PM nodes that appear more than once in the tree will have
10302 // the same depth, OSSet will refuse to add the node twice.
10303 if (inner) {
10304 ok = inner->setObject(node);
10305 }
10306 }
10307 if (!ok) {
10308 DLOG("Skipped PM node %s\n", node->getName());
10309 }
10310 }
10311 }
10312
10313 // debug only
10314 for (int i = 0; (inner = (OSSet *)gPMHaltArray->getObject(i)); i++) {
10315 count = 0;
10316 if (inner != PLACEHOLDER) {
10317 count = inner->getCount();
10318 }
10319 DLOG("Nodes at depth %u = %u\n", i, count);
10320 }
10321
10322 // strip placeholders (not all depths are populated)
10323 numWorkers = 0;
10324 for (int i = 0; (inner = (OSSet *)gPMHaltArray->getObject(i));) {
10325 if (inner == PLACEHOLDER) {
10326 gPMHaltArray->removeObject(i);
10327 continue;
10328 }
10329 count = inner->getCount();
10330 if (count > numWorkers) {
10331 numWorkers = count;
10332 }
10333 totalNodes += count;
10334 i++;
10335 }
10336
10337 if (gPMHaltArray->getCount() == 0 || !numWorkers) {
10338 goto done;
10339 }
10340
10341 gPMHaltBusyCount = 0;
10342 gPMHaltIdleCount = 0;
10343 gPMHaltDepth = gPMHaltArray->getCount() - 1;
10344
10345 // Create multiple workers (and threads)
10346
10347 if (numWorkers > kPMHaltMaxWorkers) {
10348 numWorkers = kPMHaltMaxWorkers;
10349 }
10350
10351 DLOG("PM nodes %u, maxDepth %u, workers %u\n",
10352 totalNodes, gPMHaltArray->getCount(), numWorkers);
10353
10354 for (unsigned int i = 0; i < numWorkers; i++) {
10355 workers[i] = PMHaltWorker::worker();
10356 }
10357
10358 // Wait for workers to exhaust all available work
10359
10360 IOLockLock(gPMHaltLock);
10361 while (gPMHaltDepth >= 0) {
10362 clock_interval_to_deadline(1000, kMillisecondScale, &deadline);
10363
10364 waitResult = IOLockSleepDeadline(
10365 gPMHaltLock, &gPMHaltDepth, deadline, THREAD_UNINT);
10366 if (THREAD_TIMED_OUT == waitResult) {
10367 AbsoluteTime now;
10368 clock_get_uptime(&now);
10369
10370 IOLockUnlock(gPMHaltLock);
10371 for (unsigned int i = 0; i < numWorkers; i++) {
10372 if (workers[i]) {
10373 PMHaltWorker::checkTimeout(workers[i], &now);
10374 }
10375 }
10376 IOLockLock(gPMHaltLock);
10377 }
10378 }
10379 IOLockUnlock(gPMHaltLock);
10380
10381 // Release all workers
10382
10383 for (unsigned int i = 0; i < numWorkers; i++) {
10384 if (workers[i]) {
10385 workers[i]->release();
10386 }
10387 // worker also retained by it's own thread
10388 }
10389
10390 done:
10391 DLOG("%s done\n", __FUNCTION__);
10392 return;
10393 }
10394
10395 // MARK: -
10396 // MARK: Kernel Assertion
10397
10398 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
10399
10400 IOPMDriverAssertionID
createPMAssertion(IOPMDriverAssertionType whichAssertionBits,IOPMDriverAssertionLevel assertionLevel,IOService * ownerService,const char * ownerDescription)10401 IOPMrootDomain::createPMAssertion(
10402 IOPMDriverAssertionType whichAssertionBits,
10403 IOPMDriverAssertionLevel assertionLevel,
10404 IOService *ownerService,
10405 const char *ownerDescription)
10406 {
10407 IOReturn ret;
10408 IOPMDriverAssertionID newAssertion;
10409
10410 if (!pmAssertions) {
10411 return 0;
10412 }
10413
10414 ret = pmAssertions->createAssertion(whichAssertionBits, assertionLevel, ownerService, ownerDescription, &newAssertion);
10415
10416 if (kIOReturnSuccess == ret) {
10417 #if (DEVELOPMENT || DEBUG)
10418 if (_aotNow) {
10419 OSReportWithBacktrace("IOPMrootDomain::createPMAssertion(0x%qx)", newAssertion);
10420 }
10421 #endif /* (DEVELOPMENT || DEBUG) */
10422 return newAssertion;
10423 } else {
10424 return 0;
10425 }
10426 }
10427
10428 IOReturn
releasePMAssertion(IOPMDriverAssertionID releaseAssertion)10429 IOPMrootDomain::releasePMAssertion(IOPMDriverAssertionID releaseAssertion)
10430 {
10431 #if (DEVELOPMENT || DEBUG)
10432 if (_aotNow) {
10433 OSReportWithBacktrace("IOPMrootDomain::releasePMAssertion(0x%qx)", releaseAssertion);
10434 }
10435 #endif /* (DEVELOPMENT || DEBUG) */
10436 if (!pmAssertions) {
10437 return kIOReturnInternalError;
10438 }
10439 return pmAssertions->releaseAssertion(releaseAssertion);
10440 }
10441
10442
10443 IOReturn
setPMAssertionLevel(IOPMDriverAssertionID assertionID,IOPMDriverAssertionLevel assertionLevel)10444 IOPMrootDomain::setPMAssertionLevel(
10445 IOPMDriverAssertionID assertionID,
10446 IOPMDriverAssertionLevel assertionLevel)
10447 {
10448 return pmAssertions->setAssertionLevel(assertionID, assertionLevel);
10449 }
10450
10451 IOPMDriverAssertionLevel
getPMAssertionLevel(IOPMDriverAssertionType whichAssertion)10452 IOPMrootDomain::getPMAssertionLevel(IOPMDriverAssertionType whichAssertion)
10453 {
10454 IOPMDriverAssertionType sysLevels;
10455
10456 if (!pmAssertions || whichAssertion == 0) {
10457 return kIOPMDriverAssertionLevelOff;
10458 }
10459
10460 sysLevels = pmAssertions->getActivatedAssertions();
10461
10462 // Check that every bit set in argument 'whichAssertion' is asserted
10463 // in the aggregate bits.
10464 if ((sysLevels & whichAssertion) == whichAssertion) {
10465 return kIOPMDriverAssertionLevelOn;
10466 } else {
10467 return kIOPMDriverAssertionLevelOff;
10468 }
10469 }
10470
10471 IOReturn
setPMAssertionUserLevels(IOPMDriverAssertionType inLevels)10472 IOPMrootDomain::setPMAssertionUserLevels(IOPMDriverAssertionType inLevels)
10473 {
10474 if (!pmAssertions) {
10475 return kIOReturnNotFound;
10476 }
10477
10478 return pmAssertions->setUserAssertionLevels(inLevels);
10479 }
10480
10481 IOReturn
acquireDriverKitMatchingAssertion()10482 IOPMrootDomain::acquireDriverKitMatchingAssertion()
10483 {
10484 return gIOPMWorkLoop->runActionBlock(^{
10485 if (_driverKitMatchingAssertionCount != 0) {
10486 _driverKitMatchingAssertionCount++;
10487 return kIOReturnSuccess;
10488 } else {
10489 if (kSystemTransitionSleep == _systemTransitionType) {
10490 // system going to sleep
10491 return kIOReturnBusy;
10492 } else {
10493 // createPMAssertion is asynchronous.
10494 // we must also set _driverKitMatchingAssertionCount under the PM workloop lock so that we can cancel sleep immediately
10495 // The assertion is used so that on release, we reevaluate all assertions
10496 _driverKitMatchingAssertion = createPMAssertion(kIOPMDriverAssertionCPUBit, kIOPMDriverAssertionLevelOn, this, "DK matching");
10497 if (_driverKitMatchingAssertion != kIOPMUndefinedDriverAssertionID) {
10498 _driverKitMatchingAssertionCount = 1;
10499 return kIOReturnSuccess;
10500 } else {
10501 return kIOReturnBusy;
10502 }
10503 }
10504 }
10505 });
10506 }
10507
10508 void
releaseDriverKitMatchingAssertion()10509 IOPMrootDomain::releaseDriverKitMatchingAssertion()
10510 {
10511 gIOPMWorkLoop->runActionBlock(^{
10512 if (_driverKitMatchingAssertionCount != 0) {
10513 _driverKitMatchingAssertionCount--;
10514 if (_driverKitMatchingAssertionCount == 0) {
10515 releasePMAssertion(_driverKitMatchingAssertion);
10516 _driverKitMatchingAssertion = kIOPMUndefinedDriverAssertionID;
10517 }
10518 } else {
10519 panic("Over-release of driverkit matching assertion");
10520 }
10521 return kIOReturnSuccess;
10522 });
10523 }
10524
10525 bool
serializeProperties(OSSerialize * s) const10526 IOPMrootDomain::serializeProperties( OSSerialize * s ) const
10527 {
10528 if (pmAssertions) {
10529 pmAssertions->publishProperties();
10530 }
10531 return IOService::serializeProperties(s);
10532 }
10533
10534 OSSharedPtr<OSObject>
copyProperty(const char * aKey) const10535 IOPMrootDomain::copyProperty( const char * aKey) const
10536 {
10537 OSSharedPtr<OSObject> obj;
10538 obj = IOService::copyProperty(aKey);
10539
10540 if (obj) {
10541 return obj;
10542 }
10543
10544 if (!strncmp(aKey, kIOPMSleepWakeWdogRebootKey,
10545 sizeof(kIOPMSleepWakeWdogRebootKey))) {
10546 if (swd_flags & SWD_BOOT_BY_SW_WDOG) {
10547 return OSSharedPtr<OSBoolean>(kOSBooleanTrue, OSNoRetain);
10548 } else {
10549 return OSSharedPtr<OSBoolean>(kOSBooleanFalse, OSNoRetain);
10550 }
10551 }
10552
10553 if (!strncmp(aKey, kIOPMSleepWakeWdogLogsValidKey,
10554 sizeof(kIOPMSleepWakeWdogLogsValidKey))) {
10555 if (swd_flags & SWD_VALID_LOGS) {
10556 return OSSharedPtr<OSBoolean>(kOSBooleanTrue, OSNoRetain);
10557 } else {
10558 return OSSharedPtr<OSBoolean>(kOSBooleanFalse, OSNoRetain);
10559 }
10560 }
10561
10562 /*
10563 * XXX: We should get rid of "DesktopMode" property when 'kAppleClamshellCausesSleepKey'
10564 * is set properly in darwake from sleep. For that, kIOPMEnableClamshell msg has to be
10565 * issued by DisplayWrangler on darkwake.
10566 */
10567 if (!strcmp(aKey, "DesktopMode")) {
10568 if (desktopMode) {
10569 return OSSharedPtr<OSBoolean>(kOSBooleanTrue, OSNoRetain);
10570 } else {
10571 return OSSharedPtr<OSBoolean>(kOSBooleanFalse, OSNoRetain);
10572 }
10573 }
10574 if (!strcmp(aKey, "DisplayIdleForDemandSleep")) {
10575 if (displayIdleForDemandSleep) {
10576 return OSSharedPtr<OSBoolean>(kOSBooleanTrue, OSNoRetain);
10577 } else {
10578 return OSSharedPtr<OSBoolean>(kOSBooleanFalse, OSNoRetain);
10579 }
10580 }
10581
10582 if (!strcmp(aKey, kIOPMDriverWakeEventsKey)) {
10583 OSSharedPtr<OSArray> array;
10584 WAKEEVENT_LOCK();
10585 if (_systemWakeEventsArray && _systemWakeEventsArray->getCount()) {
10586 OSSharedPtr<OSCollection> collection = _systemWakeEventsArray->copyCollection();
10587 if (collection) {
10588 array = OSDynamicPtrCast<OSArray>(collection);
10589 }
10590 }
10591 WAKEEVENT_UNLOCK();
10592 return os::move(array);
10593 }
10594
10595 if (!strcmp(aKey, kIOPMSleepStatisticsAppsKey)) {
10596 OSSharedPtr<OSArray> array;
10597 IOLockLock(pmStatsLock);
10598 if (pmStatsAppResponses && pmStatsAppResponses->getCount()) {
10599 OSSharedPtr<OSCollection> collection = pmStatsAppResponses->copyCollection();
10600 if (collection) {
10601 array = OSDynamicPtrCast<OSArray>(collection);
10602 }
10603 }
10604 IOLockUnlock(pmStatsLock);
10605 return os::move(array);
10606 }
10607
10608 if (!strcmp(aKey, kIOPMIdleSleepPreventersKey)) {
10609 OSArray *idleSleepList = NULL;
10610 gRootDomain->copySleepPreventersList(&idleSleepList, NULL);
10611 return OSSharedPtr<OSArray>(idleSleepList, OSNoRetain);
10612 }
10613
10614 if (!strcmp(aKey, kIOPMSystemSleepPreventersKey)) {
10615 OSArray *systemSleepList = NULL;
10616 gRootDomain->copySleepPreventersList(NULL, &systemSleepList);
10617 return OSSharedPtr<OSArray>(systemSleepList, OSNoRetain);
10618 }
10619
10620 if (!strcmp(aKey, kIOPMIdleSleepPreventersWithIDKey)) {
10621 OSArray *idleSleepList = NULL;
10622 gRootDomain->copySleepPreventersListWithID(&idleSleepList, NULL);
10623 return OSSharedPtr<OSArray>(idleSleepList, OSNoRetain);
10624 }
10625
10626 if (!strcmp(aKey, kIOPMSystemSleepPreventersWithIDKey)) {
10627 OSArray *systemSleepList = NULL;
10628 gRootDomain->copySleepPreventersListWithID(NULL, &systemSleepList);
10629 return OSSharedPtr<OSArray>(systemSleepList, OSNoRetain);
10630 }
10631 return NULL;
10632 }
10633
10634 // MARK: -
10635 // MARK: Wake Event Reporting
10636
10637 void
copyWakeReasonString(char * outBuf,size_t bufSize)10638 IOPMrootDomain::copyWakeReasonString( char * outBuf, size_t bufSize )
10639 {
10640 WAKEEVENT_LOCK();
10641 strlcpy(outBuf, gWakeReasonString, bufSize);
10642 WAKEEVENT_UNLOCK();
10643 }
10644
10645 void
copyShutdownReasonString(char * outBuf,size_t bufSize)10646 IOPMrootDomain::copyShutdownReasonString( char * outBuf, size_t bufSize )
10647 {
10648 WAKEEVENT_LOCK();
10649 strlcpy(outBuf, gShutdownReasonString, bufSize);
10650 WAKEEVENT_UNLOCK();
10651 }
10652
10653 //******************************************************************************
10654 // acceptSystemWakeEvents
10655 //
10656 // Private control for the acceptance of driver wake event claims.
10657 //******************************************************************************
10658
10659 void
acceptSystemWakeEvents(uint32_t control)10660 IOPMrootDomain::acceptSystemWakeEvents( uint32_t control )
10661 {
10662 bool logWakeReason = false;
10663
10664 WAKEEVENT_LOCK();
10665 switch (control) {
10666 case kAcceptSystemWakeEvents_Enable:
10667 assert(_acceptSystemWakeEvents == false);
10668 if (!_systemWakeEventsArray) {
10669 _systemWakeEventsArray = OSArray::withCapacity(4);
10670 }
10671 _acceptSystemWakeEvents = (_systemWakeEventsArray != NULL);
10672 if (!(_aotNow && (kIOPMWakeEventAOTExitFlags & _aotPendingFlags))) {
10673 gWakeReasonString[0] = '\0';
10674 if (_systemWakeEventsArray) {
10675 _systemWakeEventsArray->flushCollection();
10676 }
10677 }
10678
10679 // Remove stale WakeType property before system sleep
10680 removeProperty(kIOPMRootDomainWakeTypeKey);
10681 removeProperty(kIOPMRootDomainWakeReasonKey);
10682 break;
10683
10684 case kAcceptSystemWakeEvents_Disable:
10685 _acceptSystemWakeEvents = false;
10686 #if defined(XNU_TARGET_OS_OSX)
10687 logWakeReason = (gWakeReasonString[0] != '\0');
10688 #else /* !defined(XNU_TARGET_OS_OSX) */
10689 logWakeReason = gWakeReasonSysctlRegistered;
10690 #if DEVELOPMENT
10691 static int panic_allowed = -1;
10692
10693 if ((panic_allowed == -1) &&
10694 (PE_parse_boot_argn("swd_wakereason_panic", &panic_allowed, sizeof(panic_allowed)) == false)) {
10695 panic_allowed = 0;
10696 }
10697
10698 if (panic_allowed) {
10699 size_t i = 0;
10700 // Panic if wake reason is null or empty
10701 for (i = 0; (i < strlen(gWakeReasonString)); i++) {
10702 if ((gWakeReasonString[i] != ' ') && (gWakeReasonString[i] != '\t')) {
10703 break;
10704 }
10705 }
10706 if (i >= strlen(gWakeReasonString)) {
10707 panic("Wake reason is empty");
10708 }
10709 }
10710 #endif /* DEVELOPMENT */
10711 #endif /* !defined(XNU_TARGET_OS_OSX) */
10712
10713 // publish kIOPMRootDomainWakeReasonKey if not already set
10714 if (!propertyExists(kIOPMRootDomainWakeReasonKey)) {
10715 setProperty(kIOPMRootDomainWakeReasonKey, gWakeReasonString);
10716 }
10717 break;
10718
10719 case kAcceptSystemWakeEvents_Reenable:
10720 assert(_acceptSystemWakeEvents == false);
10721 _acceptSystemWakeEvents = (_systemWakeEventsArray != NULL);
10722 removeProperty(kIOPMRootDomainWakeReasonKey);
10723 break;
10724 }
10725 WAKEEVENT_UNLOCK();
10726
10727 if (logWakeReason) {
10728 MSG("system wake events: %s\n", gWakeReasonString);
10729 }
10730 }
10731
10732 //******************************************************************************
10733 // claimSystemWakeEvent
10734 //
10735 // For a driver to claim a device is the source/conduit of a system wake event.
10736 //******************************************************************************
10737
10738 void
claimSystemWakeEvent(IOService * device,IOOptionBits flags,const char * reason,OSObject * details)10739 IOPMrootDomain::claimSystemWakeEvent(
10740 IOService * device,
10741 IOOptionBits flags,
10742 const char * reason,
10743 OSObject * details )
10744 {
10745 OSSharedPtr<const OSSymbol> deviceName;
10746 OSSharedPtr<OSNumber> deviceRegId;
10747 OSSharedPtr<OSNumber> claimTime;
10748 OSSharedPtr<OSData> flagsData;
10749 OSSharedPtr<OSString> reasonString;
10750 OSSharedPtr<OSDictionary> dict;
10751 uint64_t timestamp;
10752 bool addWakeReason;
10753
10754 if (!device || !reason) {
10755 return;
10756 }
10757
10758 pmEventTimeStamp(×tamp);
10759
10760 IOOptionBits aotFlags = 0;
10761 bool needAOTEvaluate = FALSE;
10762
10763 if (kIOPMAOTModeAddEventFlags & _aotMode) {
10764 if (!strcmp("hold", reason)
10765 || !strcmp("help", reason)
10766 || !strcmp("menu", reason)
10767 || !strcmp("stockholm", reason)
10768 || !strcmp("ringer", reason)
10769 || !strcmp("ringerab", reason)
10770 || !strcmp("smc0", reason)
10771 || !strcmp("AOP.RTPWakeupAP", reason)
10772 || !strcmp("AOP.RTP_AP_IRQ", reason)
10773 || !strcmp("BT.OutboxNotEmpty", reason)
10774 || !strcmp("WL.OutboxNotEmpty", reason)) {
10775 flags |= kIOPMWakeEventAOTExit;
10776 }
10777 }
10778
10779 #if DEVELOPMENT || DEBUG
10780 if (_aotLingerTime && !strcmp("rtc", reason)) {
10781 flags |= kIOPMWakeEventAOTPossibleExit;
10782 }
10783 #endif /* DEVELOPMENT || DEBUG */
10784
10785 #if defined(XNU_TARGET_OS_OSX) && !DISPLAY_WRANGLER_PRESENT
10786 // Publishing the WakeType is serialized by the PM work loop
10787 if (!strcmp("rtc", reason) && (_nextScheduledAlarmType != NULL)) {
10788 pmPowerStateQueue->submitPowerEvent(kPowerEventPublishWakeType,
10789 (void *) _nextScheduledAlarmType.get());
10790 }
10791
10792 // Workaround for the missing wake HID event
10793 if (gDarkWakeFlags & kDarkWakeFlagUserWakeWorkaround) {
10794 if (!strcmp("trackpadkeyboard", reason)) {
10795 pmPowerStateQueue->submitPowerEvent(kPowerEventPublishWakeType,
10796 (void *) gIOPMWakeTypeUserKey.get());
10797 }
10798 }
10799 #endif
10800
10801 deviceName = device->copyName(gIOServicePlane);
10802 deviceRegId = OSNumber::withNumber(device->getRegistryEntryID(), 64);
10803 claimTime = OSNumber::withNumber(timestamp, 64);
10804 flagsData = OSData::withValue(flags);
10805 reasonString = OSString::withCString(reason);
10806 dict = OSDictionary::withCapacity(5 + (details ? 1 : 0));
10807 if (!dict || !deviceName || !deviceRegId || !claimTime || !flagsData || !reasonString) {
10808 goto done;
10809 }
10810
10811 dict->setObject(gIONameKey, deviceName.get());
10812 dict->setObject(gIORegistryEntryIDKey, deviceRegId.get());
10813 dict->setObject(kIOPMWakeEventTimeKey, claimTime.get());
10814 dict->setObject(kIOPMWakeEventFlagsKey, flagsData.get());
10815 dict->setObject(kIOPMWakeEventReasonKey, reasonString.get());
10816 if (details) {
10817 dict->setObject(kIOPMWakeEventDetailsKey, details);
10818 }
10819
10820 WAKEEVENT_LOCK();
10821 addWakeReason = _acceptSystemWakeEvents;
10822 if (_aotMode) {
10823 IOLog("claimSystemWakeEvent(%s, %s, 0x%x) 0x%x %d\n", reason, deviceName->getCStringNoCopy(), (int)flags, _aotPendingFlags, _aotReadyToFullWake);
10824 }
10825 aotFlags = (kIOPMWakeEventAOTFlags & flags);
10826 aotFlags = (aotFlags & ~_aotPendingFlags);
10827 needAOTEvaluate = false;
10828 if (_aotNow && aotFlags) {
10829 if (kIOPMWakeEventAOTPossibleExit & flags) {
10830 _aotMetrics->possibleCount++;
10831 }
10832 if (kIOPMWakeEventAOTConfirmedPossibleExit & flags) {
10833 _aotMetrics->confirmedPossibleCount++;
10834 }
10835 if (kIOPMWakeEventAOTRejectedPossibleExit & flags) {
10836 _aotMetrics->rejectedPossibleCount++;
10837 }
10838 if (kIOPMWakeEventAOTExpiredPossibleExit & flags) {
10839 _aotMetrics->expiredPossibleCount++;
10840 }
10841
10842 _aotPendingFlags |= aotFlags;
10843 addWakeReason = _aotNow && _systemWakeEventsArray && ((kIOPMWakeEventAOTExitFlags & aotFlags));
10844 needAOTEvaluate = _aotReadyToFullWake;
10845 }
10846 DMSG("claimSystemWakeEvent(%s, 0x%x, %s, 0x%llx) aot %d phase 0x%x add %d\n",
10847 reason, (int)flags, deviceName->getCStringNoCopy(), device->getRegistryEntryID(),
10848 _aotNow, pmTracer->getTracePhase(), addWakeReason);
10849
10850 if (!gWakeReasonSysctlRegistered) {
10851 // Lazy registration until the platform driver stops registering
10852 // the same name.
10853 gWakeReasonSysctlRegistered = true;
10854 }
10855 if (addWakeReason) {
10856 _systemWakeEventsArray->setObject(dict.get());
10857 if (gWakeReasonString[0] != '\0') {
10858 strlcat(gWakeReasonString, " ", sizeof(gWakeReasonString));
10859 }
10860 strlcat(gWakeReasonString, reason, sizeof(gWakeReasonString));
10861 }
10862
10863 WAKEEVENT_UNLOCK();
10864 if (needAOTEvaluate) {
10865 // Call aotEvaluate() on PM work loop since it may call
10866 // aotExit() which accesses PM state.
10867 pmPowerStateQueue->submitPowerEvent(kPowerEventAOTEvaluate);
10868 }
10869
10870 done:
10871 return;
10872 }
10873
10874 //******************************************************************************
10875 // claimSystemBootEvent
10876 //
10877 // For a driver to claim a device is the source/conduit of a system boot event.
10878 //******************************************************************************
10879
10880 void
claimSystemBootEvent(IOService * device,IOOptionBits flags,const char * reason,__unused OSObject * details)10881 IOPMrootDomain::claimSystemBootEvent(
10882 IOService * device,
10883 IOOptionBits flags,
10884 const char * reason,
10885 __unused OSObject * details )
10886 {
10887 if (!device || !reason) {
10888 return;
10889 }
10890
10891 DEBUG_LOG("claimSystemBootEvent(%s, %s, 0x%x)\n", reason, device->getName(), (uint32_t) flags);
10892 WAKEEVENT_LOCK();
10893 if (!gBootReasonSysctlRegistered) {
10894 // Lazy sysctl registration after setting gBootReasonString
10895 strlcat(gBootReasonString, reason, sizeof(gBootReasonString));
10896 os_atomic_store(&gBootReasonSysctlRegistered, true, release);
10897 }
10898 WAKEEVENT_UNLOCK();
10899 }
10900
10901 //******************************************************************************
10902 // claimSystemShutdownEvent
10903 //
10904 // For drivers to claim a system shutdown event on the ensuing boot.
10905 //******************************************************************************
10906
10907 void
claimSystemShutdownEvent(IOService * device,IOOptionBits flags,const char * reason,__unused OSObject * details)10908 IOPMrootDomain::claimSystemShutdownEvent(
10909 IOService * device,
10910 IOOptionBits flags,
10911 const char * reason,
10912 __unused OSObject * details )
10913 {
10914 if (!device || !reason) {
10915 return;
10916 }
10917
10918 DEBUG_LOG("claimSystemShutdownEvent(%s, %s, 0x%x)\n", reason, device->getName(), (uint32_t) flags);
10919 WAKEEVENT_LOCK();
10920 if (gShutdownReasonString[0] != '\0') {
10921 strlcat(gShutdownReasonString, " ", sizeof(gShutdownReasonString));
10922 }
10923 strlcat(gShutdownReasonString, reason, sizeof(gShutdownReasonString));
10924
10925 gShutdownReasonSysctlRegistered = true;
10926 WAKEEVENT_UNLOCK();
10927 }
10928
10929 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
10930
10931 // MARK: -
10932 // MARK: PMSettingHandle
10933
OSDefineMetaClassAndStructors(PMSettingHandle,OSObject)10934 OSDefineMetaClassAndStructors( PMSettingHandle, OSObject )
10935
10936 void
10937 PMSettingHandle::free( void )
10938 {
10939 if (pmso) {
10940 pmso->clientHandleFreed();
10941 pmso->release();
10942 pmso = NULL;
10943 }
10944
10945 OSObject::free();
10946 }
10947
10948 // MARK: -
10949 // MARK: PMSettingObject
10950
10951 #undef super
10952 #define super OSObject
OSDefineMetaClassAndFinalStructors(PMSettingObject,OSObject)10953 OSDefineMetaClassAndFinalStructors( PMSettingObject, OSObject )
10954
10955 /*
10956 * Static constructor/initializer for PMSettingObject
10957 */
10958 PMSettingObject *PMSettingObject::pmSettingObject(
10959 IOPMrootDomain * parent_arg,
10960 IOPMSettingControllerCallback handler_arg,
10961 OSObject * target_arg,
10962 uintptr_t refcon_arg,
10963 uint32_t supportedPowerSources,
10964 const OSSymbol * settings[],
10965 OSObject * *handle_obj)
10966 {
10967 uint32_t settingCount = 0;
10968 PMSettingObject *pmso = NULL;
10969 PMSettingHandle *pmsh = NULL;
10970
10971 if (!parent_arg || !handler_arg || !settings || !handle_obj) {
10972 return NULL;
10973 }
10974
10975 // count OSSymbol entries in NULL terminated settings array
10976 while (settings[settingCount]) {
10977 settingCount++;
10978 }
10979 if (0 == settingCount) {
10980 return NULL;
10981 }
10982
10983 pmso = new PMSettingObject;
10984 if (!pmso || !pmso->init()) {
10985 goto fail;
10986 }
10987
10988 pmsh = new PMSettingHandle;
10989 if (!pmsh || !pmsh->init()) {
10990 goto fail;
10991 }
10992
10993 queue_init(&pmso->calloutQueue);
10994 pmso->parent = parent_arg;
10995 pmso->func = handler_arg;
10996 pmso->target = target_arg;
10997 pmso->refcon = refcon_arg;
10998 pmso->settingCount = settingCount;
10999
11000 pmso->retain(); // handle holds a retain on pmso
11001 pmsh->pmso = pmso;
11002 pmso->pmsh = pmsh;
11003
11004 pmso->publishedFeatureID = OSDataAllocation<uint32_t>(settingCount, OSAllocateMemory);
11005 if (pmso->publishedFeatureID) {
11006 for (unsigned int i = 0; i < settingCount; i++) {
11007 // Since there is now at least one listener to this setting, publish
11008 // PM root domain support for it.
11009 parent_arg->publishPMSetting( settings[i],
11010 supportedPowerSources, &pmso->publishedFeatureID[i] );
11011 }
11012 }
11013
11014 *handle_obj = pmsh;
11015 return pmso;
11016
11017 fail:
11018 if (pmso) {
11019 pmso->release();
11020 }
11021 if (pmsh) {
11022 pmsh->release();
11023 }
11024 return NULL;
11025 }
11026
11027 void
free(void)11028 PMSettingObject::free( void )
11029 {
11030 if (publishedFeatureID) {
11031 for (const auto& featureID : publishedFeatureID) {
11032 if (featureID) {
11033 parent->removePublishedFeature( featureID );
11034 }
11035 }
11036
11037 publishedFeatureID = {};
11038 }
11039
11040 super::free();
11041 }
11042
11043 IOReturn
dispatchPMSetting(const OSSymbol * type,OSObject * object)11044 PMSettingObject::dispatchPMSetting( const OSSymbol * type, OSObject * object )
11045 {
11046 return (*func)(target, type, object, refcon);
11047 }
11048
11049 void
clientHandleFreed(void)11050 PMSettingObject::clientHandleFreed( void )
11051 {
11052 parent->deregisterPMSettingObject(this);
11053 }
11054
11055 // MARK: -
11056 // MARK: PMAssertionsTracker
11057
11058 //*********************************************************************************
11059 //*********************************************************************************
11060 //*********************************************************************************
11061 // class PMAssertionsTracker Implementation
11062
11063 #define kAssertUniqueIDStart 500
11064
11065 PMAssertionsTracker *
pmAssertionsTracker(IOPMrootDomain * rootDomain)11066 PMAssertionsTracker::pmAssertionsTracker( IOPMrootDomain *rootDomain )
11067 {
11068 PMAssertionsTracker *me;
11069
11070 me = new PMAssertionsTracker;
11071 if (!me || !me->init()) {
11072 if (me) {
11073 me->release();
11074 }
11075 return NULL;
11076 }
11077
11078 me->owner = rootDomain;
11079 me->issuingUniqueID = kAssertUniqueIDStart;
11080 me->assertionsArray = OSArray::withCapacity(5);
11081 me->assertionsKernel = 0;
11082 me->assertionsUser = 0;
11083 me->assertionsCombined = 0;
11084 me->assertionsArrayLock = IOLockAlloc();
11085 me->tabulateProducerCount = me->tabulateConsumerCount = 0;
11086
11087 assert(me->assertionsArray);
11088 assert(me->assertionsArrayLock);
11089
11090 return me;
11091 }
11092
11093 /* tabulate
11094 * - Update assertionsKernel to reflect the state of all
11095 * assertions in the kernel.
11096 * - Update assertionsCombined to reflect both kernel & user space.
11097 */
11098 void
tabulate(void)11099 PMAssertionsTracker::tabulate(void)
11100 {
11101 int i;
11102 int count;
11103 const PMAssertStruct *_a = nullptr;
11104 OSValueObject<PMAssertStruct> *_d = nullptr;
11105
11106 IOPMDriverAssertionType oldKernel = assertionsKernel;
11107 IOPMDriverAssertionType oldCombined = assertionsCombined;
11108
11109 ASSERT_GATED();
11110
11111 assertionsKernel = 0;
11112 assertionsCombined = 0;
11113
11114 if (!assertionsArray) {
11115 return;
11116 }
11117
11118 if ((count = assertionsArray->getCount())) {
11119 for (i = 0; i < count; i++) {
11120 _d = OSDynamicCast(OSValueObject<PMAssertStruct>, assertionsArray->getObject(i));
11121 if (_d) {
11122 _a = _d->getBytesNoCopy();
11123 if (_a && (kIOPMDriverAssertionLevelOn == _a->level)) {
11124 assertionsKernel |= _a->assertionBits;
11125 }
11126 }
11127 }
11128 }
11129
11130 tabulateProducerCount++;
11131 assertionsCombined = assertionsKernel | assertionsUser;
11132
11133 if ((assertionsKernel != oldKernel) ||
11134 (assertionsCombined != oldCombined)) {
11135 owner->evaluateAssertions(assertionsCombined, oldCombined);
11136 }
11137 }
11138
11139 void
updateCPUBitAccounting(PMAssertStruct * assertStruct)11140 PMAssertionsTracker::updateCPUBitAccounting( PMAssertStruct *assertStruct )
11141 {
11142 AbsoluteTime now;
11143 uint64_t nsec;
11144
11145 if (((assertStruct->assertionBits & kIOPMDriverAssertionCPUBit) == 0) ||
11146 (assertStruct->assertCPUStartTime == 0)) {
11147 return;
11148 }
11149
11150 now = mach_absolute_time();
11151 SUB_ABSOLUTETIME(&now, &assertStruct->assertCPUStartTime);
11152 absolutetime_to_nanoseconds(now, &nsec);
11153 assertStruct->assertCPUDuration += nsec;
11154 assertStruct->assertCPUStartTime = 0;
11155
11156 if (assertStruct->assertCPUDuration > maxAssertCPUDuration) {
11157 maxAssertCPUDuration = assertStruct->assertCPUDuration;
11158 maxAssertCPUEntryId = assertStruct->registryEntryID;
11159 }
11160 }
11161
11162 void
reportCPUBitAccounting(void)11163 PMAssertionsTracker::reportCPUBitAccounting( void )
11164 {
11165 const PMAssertStruct *_a = nullptr;
11166 OSValueObject<PMAssertStruct> *_d = nullptr;
11167 int i, count;
11168 AbsoluteTime now;
11169 uint64_t nsec;
11170
11171 ASSERT_GATED();
11172
11173 // Account for drivers that are still holding the CPU assertion
11174 if (assertionsKernel & kIOPMDriverAssertionCPUBit) {
11175 now = mach_absolute_time();
11176 if ((count = assertionsArray->getCount())) {
11177 for (i = 0; i < count; i++) {
11178 _d = OSDynamicCast(OSValueObject<PMAssertStruct>, assertionsArray->getObject(i));
11179 if (_d) {
11180 _a = _d->getBytesNoCopy();
11181 if ((_a->assertionBits & kIOPMDriverAssertionCPUBit) &&
11182 (_a->level == kIOPMDriverAssertionLevelOn) &&
11183 (_a->assertCPUStartTime != 0)) {
11184 // Don't modify PMAssertStruct, leave that
11185 // for updateCPUBitAccounting()
11186 SUB_ABSOLUTETIME(&now, &_a->assertCPUStartTime);
11187 absolutetime_to_nanoseconds(now, &nsec);
11188 nsec += _a->assertCPUDuration;
11189 if (nsec > maxAssertCPUDuration) {
11190 maxAssertCPUDuration = nsec;
11191 maxAssertCPUEntryId = _a->registryEntryID;
11192 }
11193 }
11194 }
11195 }
11196 }
11197 }
11198
11199 if (maxAssertCPUDuration) {
11200 DLOG("cpu assertion held for %llu ms by 0x%llx\n",
11201 (maxAssertCPUDuration / NSEC_PER_MSEC), maxAssertCPUEntryId);
11202 }
11203
11204 maxAssertCPUDuration = 0;
11205 maxAssertCPUEntryId = 0;
11206 }
11207
11208 void
publishProperties(void)11209 PMAssertionsTracker::publishProperties( void )
11210 {
11211 OSSharedPtr<OSArray> assertionsSummary;
11212
11213 if (tabulateConsumerCount != tabulateProducerCount) {
11214 IOLockLock(assertionsArrayLock);
11215
11216 tabulateConsumerCount = tabulateProducerCount;
11217
11218 /* Publish the IOPMrootDomain property "DriverPMAssertionsDetailed"
11219 */
11220 assertionsSummary = copyAssertionsArray();
11221 if (assertionsSummary) {
11222 owner->setProperty(kIOPMAssertionsDriverDetailedKey, assertionsSummary.get());
11223 } else {
11224 owner->removeProperty(kIOPMAssertionsDriverDetailedKey);
11225 }
11226
11227 /* Publish the IOPMrootDomain property "DriverPMAssertions"
11228 */
11229 owner->setProperty(kIOPMAssertionsDriverKey, assertionsKernel, 64);
11230
11231 IOLockUnlock(assertionsArrayLock);
11232 }
11233 }
11234
11235 PMAssertStruct *
detailsForID(IOPMDriverAssertionID _id,int * index)11236 PMAssertionsTracker::detailsForID(IOPMDriverAssertionID _id, int *index)
11237 {
11238 PMAssertStruct *_a = NULL;
11239 OSValueObject<PMAssertStruct> *_d = nullptr;
11240 int found = -1;
11241 int count = 0;
11242 int i = 0;
11243
11244 if (assertionsArray
11245 && (count = assertionsArray->getCount())) {
11246 for (i = 0; i < count; i++) {
11247 _d = OSDynamicCast(OSValueObject<PMAssertStruct>, assertionsArray->getObject(i));
11248 if (_d) {
11249 _a = _d->getMutableBytesNoCopy();
11250 if (_a && (_id == _a->id)) {
11251 found = i;
11252 break;
11253 }
11254 }
11255 }
11256 }
11257
11258 if (-1 == found) {
11259 return NULL;
11260 } else {
11261 if (index) {
11262 *index = found;
11263 }
11264 return _a;
11265 }
11266 }
11267
11268 /* PMAssertionsTracker::handleCreateAssertion
11269 * Perform assertion work on the PM workloop. Do not call directly.
11270 */
11271 IOReturn
handleCreateAssertion(OSValueObject<PMAssertStruct> * newAssertion)11272 PMAssertionsTracker::handleCreateAssertion(OSValueObject<PMAssertStruct> *newAssertion)
11273 {
11274 PMAssertStruct *assertStruct = nullptr;
11275
11276 ASSERT_GATED();
11277
11278 if (newAssertion) {
11279 IOLockLock(assertionsArrayLock);
11280 assertStruct = newAssertion->getMutableBytesNoCopy();
11281 if ((assertStruct->assertionBits & kIOPMDriverAssertionCPUBit) &&
11282 (assertStruct->level == kIOPMDriverAssertionLevelOn)) {
11283 assertStruct->assertCPUStartTime = mach_absolute_time();
11284 }
11285 assertionsArray->setObject(newAssertion);
11286 IOLockUnlock(assertionsArrayLock);
11287 newAssertion->release();
11288
11289 tabulate();
11290 }
11291 return kIOReturnSuccess;
11292 }
11293
11294 /* PMAssertionsTracker::createAssertion
11295 * createAssertion allocates memory for a new PM assertion, and affects system behavior, if
11296 * appropiate.
11297 */
11298 IOReturn
createAssertion(IOPMDriverAssertionType which,IOPMDriverAssertionLevel level,IOService * serviceID,const char * whoItIs,IOPMDriverAssertionID * outID)11299 PMAssertionsTracker::createAssertion(
11300 IOPMDriverAssertionType which,
11301 IOPMDriverAssertionLevel level,
11302 IOService *serviceID,
11303 const char *whoItIs,
11304 IOPMDriverAssertionID *outID)
11305 {
11306 OSSharedPtr<OSValueObject<PMAssertStruct> > dataStore;
11307 PMAssertStruct track;
11308
11309 // Warning: trillions and trillions of created assertions may overflow the unique ID.
11310 track.id = OSIncrementAtomic64((SInt64*) &issuingUniqueID);
11311 track.level = level;
11312 track.assertionBits = which;
11313
11314 // NB: ownerString is explicitly managed by PMAssertStruct
11315 // it will be released in `handleReleaseAssertion' below
11316 track.ownerString = whoItIs ? OSSymbol::withCString(whoItIs).detach():nullptr;
11317 track.ownerService = serviceID;
11318 track.registryEntryID = serviceID ? serviceID->getRegistryEntryID():0;
11319 track.modifiedTime = 0;
11320 pmEventTimeStamp(&track.createdTime);
11321 track.assertCPUStartTime = 0;
11322 track.assertCPUDuration = 0;
11323
11324 dataStore = OSValueObjectWithValue(track);
11325 if (!dataStore) {
11326 if (track.ownerString) {
11327 track.ownerString->release();
11328 track.ownerString = NULL;
11329 }
11330 return kIOReturnNoMemory;
11331 }
11332
11333 *outID = track.id;
11334
11335 if (owner && owner->pmPowerStateQueue) {
11336 // queue action is responsible for releasing dataStore
11337 owner->pmPowerStateQueue->submitPowerEvent(kPowerEventAssertionCreate, (void *)dataStore.detach());
11338 }
11339
11340 return kIOReturnSuccess;
11341 }
11342
11343 /* PMAssertionsTracker::handleReleaseAssertion
11344 * Runs in PM workloop. Do not call directly.
11345 */
11346 IOReturn
handleReleaseAssertion(IOPMDriverAssertionID _id)11347 PMAssertionsTracker::handleReleaseAssertion(
11348 IOPMDriverAssertionID _id)
11349 {
11350 ASSERT_GATED();
11351
11352 int index;
11353 PMAssertStruct *assertStruct = detailsForID(_id, &index);
11354
11355 if (!assertStruct) {
11356 return kIOReturnNotFound;
11357 }
11358
11359 IOLockLock(assertionsArrayLock);
11360
11361 if ((assertStruct->assertionBits & kIOPMDriverAssertionCPUBit) &&
11362 (assertStruct->level == kIOPMDriverAssertionLevelOn)) {
11363 updateCPUBitAccounting(assertStruct);
11364 }
11365
11366 if (assertStruct->ownerString) {
11367 assertStruct->ownerString->release();
11368 assertStruct->ownerString = NULL;
11369 }
11370
11371 assertionsArray->removeObject(index);
11372 IOLockUnlock(assertionsArrayLock);
11373
11374 tabulate();
11375 return kIOReturnSuccess;
11376 }
11377
11378 /* PMAssertionsTracker::releaseAssertion
11379 * Releases an assertion and affects system behavior if appropiate.
11380 * Actual work happens on PM workloop.
11381 */
11382 IOReturn
releaseAssertion(IOPMDriverAssertionID _id)11383 PMAssertionsTracker::releaseAssertion(
11384 IOPMDriverAssertionID _id)
11385 {
11386 if (owner && owner->pmPowerStateQueue) {
11387 owner->pmPowerStateQueue->submitPowerEvent(kPowerEventAssertionRelease, NULL, _id);
11388 }
11389 return kIOReturnSuccess;
11390 }
11391
11392 /* PMAssertionsTracker::handleSetAssertionLevel
11393 * Runs in PM workloop. Do not call directly.
11394 */
11395 IOReturn
handleSetAssertionLevel(IOPMDriverAssertionID _id,IOPMDriverAssertionLevel _level)11396 PMAssertionsTracker::handleSetAssertionLevel(
11397 IOPMDriverAssertionID _id,
11398 IOPMDriverAssertionLevel _level)
11399 {
11400 PMAssertStruct *assertStruct = detailsForID(_id, NULL);
11401
11402 ASSERT_GATED();
11403
11404 if (!assertStruct) {
11405 return kIOReturnNotFound;
11406 }
11407
11408 IOLockLock(assertionsArrayLock);
11409 pmEventTimeStamp(&assertStruct->modifiedTime);
11410 if ((assertStruct->assertionBits & kIOPMDriverAssertionCPUBit) &&
11411 (assertStruct->level != _level)) {
11412 if (_level == kIOPMDriverAssertionLevelOn) {
11413 assertStruct->assertCPUStartTime = mach_absolute_time();
11414 } else {
11415 updateCPUBitAccounting(assertStruct);
11416 }
11417 }
11418 assertStruct->level = _level;
11419 IOLockUnlock(assertionsArrayLock);
11420
11421 tabulate();
11422 return kIOReturnSuccess;
11423 }
11424
11425 /* PMAssertionsTracker::setAssertionLevel
11426 */
11427 IOReturn
setAssertionLevel(IOPMDriverAssertionID _id,IOPMDriverAssertionLevel _level)11428 PMAssertionsTracker::setAssertionLevel(
11429 IOPMDriverAssertionID _id,
11430 IOPMDriverAssertionLevel _level)
11431 {
11432 if (owner && owner->pmPowerStateQueue) {
11433 owner->pmPowerStateQueue->submitPowerEvent(kPowerEventAssertionSetLevel,
11434 (void *)(uintptr_t)_level, _id);
11435 }
11436
11437 return kIOReturnSuccess;
11438 }
11439
11440 IOReturn
handleSetUserAssertionLevels(void * arg0)11441 PMAssertionsTracker::handleSetUserAssertionLevels(void * arg0)
11442 {
11443 IOPMDriverAssertionType new_user_levels = *(IOPMDriverAssertionType *) arg0;
11444
11445 ASSERT_GATED();
11446
11447 if (new_user_levels != assertionsUser) {
11448 DLOG("assertionsUser 0x%llx->0x%llx\n", assertionsUser, new_user_levels);
11449 assertionsUser = new_user_levels;
11450 }
11451
11452 tabulate();
11453 return kIOReturnSuccess;
11454 }
11455
11456 IOReturn
setUserAssertionLevels(IOPMDriverAssertionType new_user_levels)11457 PMAssertionsTracker::setUserAssertionLevels(
11458 IOPMDriverAssertionType new_user_levels)
11459 {
11460 if (gIOPMWorkLoop) {
11461 gIOPMWorkLoop->runAction(
11462 OSMemberFunctionCast(
11463 IOWorkLoop::Action,
11464 this,
11465 &PMAssertionsTracker::handleSetUserAssertionLevels),
11466 this,
11467 (void *) &new_user_levels, NULL, NULL, NULL);
11468 }
11469
11470 return kIOReturnSuccess;
11471 }
11472
11473
11474 OSSharedPtr<OSArray>
copyAssertionsArray(void)11475 PMAssertionsTracker::copyAssertionsArray(void)
11476 {
11477 int count;
11478 int i;
11479 OSSharedPtr<OSArray> outArray = NULL;
11480
11481 if (!assertionsArray || (0 == (count = assertionsArray->getCount()))) {
11482 goto exit;
11483 }
11484 outArray = OSArray::withCapacity(count);
11485 if (!outArray) {
11486 goto exit;
11487 }
11488
11489 for (i = 0; i < count; i++) {
11490 const PMAssertStruct *_a = nullptr;
11491 OSValueObject<PMAssertStruct> *_d = nullptr;
11492 OSSharedPtr<OSDictionary> details;
11493
11494 _d = OSDynamicCast(OSValueObject<PMAssertStruct>, assertionsArray->getObject(i));
11495 if (_d && (_a = _d->getBytesNoCopy())) {
11496 OSSharedPtr<OSNumber> _n;
11497
11498 details = OSDictionary::withCapacity(7);
11499 if (!details) {
11500 continue;
11501 }
11502
11503 outArray->setObject(details.get());
11504
11505 _n = OSNumber::withNumber(_a->id, 64);
11506 if (_n) {
11507 details->setObject(kIOPMDriverAssertionIDKey, _n.get());
11508 }
11509 _n = OSNumber::withNumber(_a->createdTime, 64);
11510 if (_n) {
11511 details->setObject(kIOPMDriverAssertionCreatedTimeKey, _n.get());
11512 }
11513 _n = OSNumber::withNumber(_a->modifiedTime, 64);
11514 if (_n) {
11515 details->setObject(kIOPMDriverAssertionModifiedTimeKey, _n.get());
11516 }
11517 _n = OSNumber::withNumber((uintptr_t)_a->registryEntryID, 64);
11518 if (_n) {
11519 details->setObject(kIOPMDriverAssertionRegistryEntryIDKey, _n.get());
11520 }
11521 _n = OSNumber::withNumber(_a->level, 64);
11522 if (_n) {
11523 details->setObject(kIOPMDriverAssertionLevelKey, _n.get());
11524 }
11525 _n = OSNumber::withNumber(_a->assertionBits, 64);
11526 if (_n) {
11527 details->setObject(kIOPMDriverAssertionAssertedKey, _n.get());
11528 }
11529
11530 if (_a->ownerString) {
11531 details->setObject(kIOPMDriverAssertionOwnerStringKey, _a->ownerString);
11532 }
11533 }
11534 }
11535
11536 exit:
11537 return os::move(outArray);
11538 }
11539
11540 IOPMDriverAssertionType
getActivatedAssertions(void)11541 PMAssertionsTracker::getActivatedAssertions(void)
11542 {
11543 return assertionsCombined;
11544 }
11545
11546 IOPMDriverAssertionLevel
getAssertionLevel(IOPMDriverAssertionType type)11547 PMAssertionsTracker::getAssertionLevel(
11548 IOPMDriverAssertionType type)
11549 {
11550 // FIXME: unused and also wrong
11551 if (type && ((type & assertionsKernel) == assertionsKernel)) {
11552 return kIOPMDriverAssertionLevelOn;
11553 } else {
11554 return kIOPMDriverAssertionLevelOff;
11555 }
11556 }
11557
11558 //*********************************************************************************
11559 //*********************************************************************************
11560 //*********************************************************************************
11561
11562
11563 static void
pmEventTimeStamp(uint64_t * recordTS)11564 pmEventTimeStamp(uint64_t *recordTS)
11565 {
11566 clock_sec_t tsec;
11567 clock_usec_t tusec;
11568
11569 if (!recordTS) {
11570 return;
11571 }
11572
11573 // We assume tsec fits into 32 bits; 32 bits holds enough
11574 // seconds for 136 years since the epoch in 1970.
11575 clock_get_calendar_microtime(&tsec, &tusec);
11576
11577
11578 // Pack the sec & microsec calendar time into a uint64_t, for fun.
11579 *recordTS = 0;
11580 *recordTS |= (uint32_t)tusec;
11581 *recordTS |= ((uint64_t)tsec << 32);
11582
11583 return;
11584 }
11585
11586 // MARK: -
11587 // MARK: IORootParent
11588
11589 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
11590
11591 OSDefineMetaClassAndFinalStructors(IORootParent, IOService)
11592
11593 // The reason that root domain needs a root parent is to facilitate demand
11594 // sleep, since a power change from the root parent cannot be vetoed.
11595 //
11596 // The above statement is no longer true since root domain now performs
11597 // demand sleep using overrides. But root parent remains to avoid changing
11598 // the power tree stacking. Root parent is parked at the max power state.
11599
11600
11601 static IOPMPowerState patriarchPowerStates[2] =
11602 {
11603 {1, 0, ON_POWER, 0, 0, 0, 0, 0, 0, 0, 0, 0},
11604 {1, 0, ON_POWER, 0, 0, 0, 0, 0, 0, 0, 0, 0},
11605 };
11606
11607 void
initialize(void)11608 IORootParent::initialize( void )
11609 {
11610
11611 gIOPMPSExternalConnectedKey = OSSymbol::withCStringNoCopy(kIOPMPSExternalConnectedKey);
11612 gIOPMPSExternalChargeCapableKey = OSSymbol::withCStringNoCopy(kIOPMPSExternalChargeCapableKey);
11613 gIOPMPSBatteryInstalledKey = OSSymbol::withCStringNoCopy(kIOPMPSBatteryInstalledKey);
11614 gIOPMPSIsChargingKey = OSSymbol::withCStringNoCopy(kIOPMPSIsChargingKey);
11615 gIOPMPSAtWarnLevelKey = OSSymbol::withCStringNoCopy(kIOPMPSAtWarnLevelKey);
11616 gIOPMPSAtCriticalLevelKey = OSSymbol::withCStringNoCopy(kIOPMPSAtCriticalLevelKey);
11617 gIOPMPSCurrentCapacityKey = OSSymbol::withCStringNoCopy(kIOPMPSCurrentCapacityKey);
11618 gIOPMPSMaxCapacityKey = OSSymbol::withCStringNoCopy(kIOPMPSMaxCapacityKey);
11619 gIOPMPSDesignCapacityKey = OSSymbol::withCStringNoCopy(kIOPMPSDesignCapacityKey);
11620 gIOPMPSTimeRemainingKey = OSSymbol::withCStringNoCopy(kIOPMPSTimeRemainingKey);
11621 gIOPMPSAmperageKey = OSSymbol::withCStringNoCopy(kIOPMPSAmperageKey);
11622 gIOPMPSVoltageKey = OSSymbol::withCStringNoCopy(kIOPMPSVoltageKey);
11623 gIOPMPSCycleCountKey = OSSymbol::withCStringNoCopy(kIOPMPSCycleCountKey);
11624 gIOPMPSMaxErrKey = OSSymbol::withCStringNoCopy(kIOPMPSMaxErrKey);
11625 gIOPMPSAdapterInfoKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterInfoKey);
11626 gIOPMPSLocationKey = OSSymbol::withCStringNoCopy(kIOPMPSLocationKey);
11627 gIOPMPSErrorConditionKey = OSSymbol::withCStringNoCopy(kIOPMPSErrorConditionKey);
11628 gIOPMPSManufacturerKey = OSSymbol::withCStringNoCopy(kIOPMPSManufacturerKey);
11629 gIOPMPSManufactureDateKey = OSSymbol::withCStringNoCopy(kIOPMPSManufactureDateKey);
11630 gIOPMPSModelKey = OSSymbol::withCStringNoCopy(kIOPMPSModelKey);
11631 gIOPMPSSerialKey = OSSymbol::withCStringNoCopy(kIOPMPSSerialKey);
11632 gIOPMPSLegacyBatteryInfoKey = OSSymbol::withCStringNoCopy(kIOPMPSLegacyBatteryInfoKey);
11633 gIOPMPSBatteryHealthKey = OSSymbol::withCStringNoCopy(kIOPMPSBatteryHealthKey);
11634 gIOPMPSHealthConfidenceKey = OSSymbol::withCStringNoCopy(kIOPMPSHealthConfidenceKey);
11635 gIOPMPSCapacityEstimatedKey = OSSymbol::withCStringNoCopy(kIOPMPSCapacityEstimatedKey);
11636 gIOPMPSBatteryChargeStatusKey = OSSymbol::withCStringNoCopy(kIOPMPSBatteryChargeStatusKey);
11637 gIOPMPSBatteryTemperatureKey = OSSymbol::withCStringNoCopy(kIOPMPSBatteryTemperatureKey);
11638 gIOPMPSAdapterDetailsKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsKey);
11639 gIOPMPSChargerConfigurationKey = OSSymbol::withCStringNoCopy(kIOPMPSChargerConfigurationKey);
11640 gIOPMPSAdapterDetailsIDKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsIDKey);
11641 gIOPMPSAdapterDetailsWattsKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsWattsKey);
11642 gIOPMPSAdapterDetailsRevisionKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsRevisionKey);
11643 gIOPMPSAdapterDetailsSerialNumberKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsSerialNumberKey);
11644 gIOPMPSAdapterDetailsFamilyKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsFamilyKey);
11645 gIOPMPSAdapterDetailsAmperageKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsAmperageKey);
11646 gIOPMPSAdapterDetailsDescriptionKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsDescriptionKey);
11647 gIOPMPSAdapterDetailsPMUConfigurationKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsPMUConfigurationKey);
11648 gIOPMPSAdapterDetailsSourceIDKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsSourceIDKey);
11649 gIOPMPSAdapterDetailsErrorFlagsKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsErrorFlagsKey);
11650 gIOPMPSAdapterDetailsSharedSourceKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsSharedSourceKey);
11651 gIOPMPSAdapterDetailsCloakedKey = OSSymbol::withCStringNoCopy(kIOPMPSAdapterDetailsCloakedKey);
11652 gIOPMPSInvalidWakeSecondsKey = OSSymbol::withCStringNoCopy(kIOPMPSInvalidWakeSecondsKey);
11653 gIOPMPSPostChargeWaitSecondsKey = OSSymbol::withCStringNoCopy(kIOPMPSPostChargeWaitSecondsKey);
11654 gIOPMPSPostDishargeWaitSecondsKey = OSSymbol::withCStringNoCopy(kIOPMPSPostDishargeWaitSecondsKey);
11655 }
11656
11657 bool
start(IOService * nub)11658 IORootParent::start( IOService * nub )
11659 {
11660 IOService::start(nub);
11661 attachToParent( getRegistryRoot(), gIOPowerPlane );
11662 PMinit();
11663 registerPowerDriver(this, patriarchPowerStates, 2);
11664 makeUsable();
11665 return true;
11666 }
11667
11668 void
shutDownSystem(void)11669 IORootParent::shutDownSystem( void )
11670 {
11671 }
11672
11673 void
restartSystem(void)11674 IORootParent::restartSystem( void )
11675 {
11676 }
11677
11678 void
sleepSystem(void)11679 IORootParent::sleepSystem( void )
11680 {
11681 }
11682
11683 void
dozeSystem(void)11684 IORootParent::dozeSystem( void )
11685 {
11686 }
11687
11688 void
sleepToDoze(void)11689 IORootParent::sleepToDoze( void )
11690 {
11691 }
11692
11693 void
wakeSystem(void)11694 IORootParent::wakeSystem( void )
11695 {
11696 }
11697
11698 OSSharedPtr<OSObject>
copyProperty(const char * aKey) const11699 IORootParent::copyProperty( const char * aKey) const
11700 {
11701 return IOService::copyProperty(aKey);
11702 }
11703
11704 uint32_t
getWatchdogTimeout()11705 IOPMrootDomain::getWatchdogTimeout()
11706 {
11707 if (gSwdSleepWakeTimeout) {
11708 gSwdSleepTimeout = gSwdWakeTimeout = gSwdSleepWakeTimeout;
11709 }
11710 if ((pmTracer->getTracePhase() < kIOPMTracePointSystemSleep) ||
11711 (pmTracer->getTracePhase() == kIOPMTracePointDarkWakeEntry)) {
11712 return gSwdSleepTimeout ? gSwdSleepTimeout : WATCHDOG_SLEEP_TIMEOUT;
11713 } else {
11714 return gSwdWakeTimeout ? gSwdWakeTimeout : WATCHDOG_WAKE_TIMEOUT;
11715 }
11716 }
11717
11718
11719 #if defined(__i386__) || defined(__x86_64__) || (defined(__arm64__) && HIBERNATION)
11720 IOReturn
restartWithStackshot()11721 IOPMrootDomain::restartWithStackshot()
11722 {
11723 takeStackshot(true);
11724
11725 return kIOReturnSuccess;
11726 }
11727
11728 void
sleepWakeDebugTrig(bool wdogTrigger)11729 IOPMrootDomain::sleepWakeDebugTrig(bool wdogTrigger)
11730 {
11731 takeStackshot(wdogTrigger);
11732 }
11733
11734 void
tracePhase2String(uint32_t tracePhase,const char ** phaseString,const char ** description)11735 IOPMrootDomain::tracePhase2String(uint32_t tracePhase, const char **phaseString, const char **description)
11736 {
11737 switch (tracePhase) {
11738 case kIOPMTracePointSleepStarted:
11739 *phaseString = "kIOPMTracePointSleepStarted";
11740 *description = "starting sleep";
11741 break;
11742
11743 case kIOPMTracePointSleepApplications:
11744 *phaseString = "kIOPMTracePointSleepApplications";
11745 *description = "notifying applications";
11746 break;
11747
11748 case kIOPMTracePointSleepPriorityClients:
11749 *phaseString = "kIOPMTracePointSleepPriorityClients";
11750 *description = "notifying clients about upcoming system capability changes";
11751 break;
11752
11753 case kIOPMTracePointSleepWillChangeInterests:
11754 *phaseString = "kIOPMTracePointSleepWillChangeInterests";
11755 *description = "creating hibernation file or while calling rootDomain's clients about upcoming rootDomain's state changes";
11756 break;
11757
11758 case kIOPMTracePointSleepPowerPlaneDrivers:
11759 *phaseString = "kIOPMTracePointSleepPowerPlaneDrivers";
11760 *description = "calling power state change callbacks";
11761 break;
11762
11763 case kIOPMTracePointSleepDidChangeInterests:
11764 *phaseString = "kIOPMTracePointSleepDidChangeInterests";
11765 *description = "calling rootDomain's clients about rootDomain's state changes";
11766 break;
11767
11768 case kIOPMTracePointSleepCapabilityClients:
11769 *phaseString = "kIOPMTracePointSleepCapabilityClients";
11770 *description = "notifying clients about current system capabilities";
11771 break;
11772
11773 case kIOPMTracePointSleepPlatformActions:
11774 *phaseString = "kIOPMTracePointSleepPlatformActions";
11775 *description = "calling Quiesce/Sleep action callbacks";
11776 break;
11777
11778 case kIOPMTracePointSleepCPUs:
11779 {
11780 *phaseString = "kIOPMTracePointSleepCPUs";
11781 #if defined(__i386__) || defined(__x86_64__)
11782 /*
11783 * We cannot use the getCPUNumber() method to get the cpu number, since
11784 * that cpu number is unrelated to the cpu number we need (we need the cpu
11785 * number as enumerated by the scheduler, NOT the CPU number enumerated
11786 * by ACPIPlatform as the CPUs are enumerated in MADT order).
11787 * Instead, pass the Mach processor pointer associated with the current
11788 * shutdown target so its associated cpu_id can be used in
11789 * processor_to_datastring.
11790 */
11791 if (currentShutdownTarget != NULL &&
11792 currentShutdownTarget->getMachProcessor() != NULL) {
11793 const char *sbuf = processor_to_datastring("halting all non-boot CPUs",
11794 currentShutdownTarget->getMachProcessor());
11795 *description = sbuf;
11796 } else {
11797 *description = "halting all non-boot CPUs";
11798 }
11799 #else
11800 *description = "halting all non-boot CPUs";
11801 #endif
11802 break;
11803 }
11804 case kIOPMTracePointSleepPlatformDriver:
11805 *phaseString = "kIOPMTracePointSleepPlatformDriver";
11806 *description = "executing platform specific code";
11807 break;
11808
11809 case kIOPMTracePointHibernate:
11810 *phaseString = "kIOPMTracePointHibernate";
11811 *description = "writing the hibernation image";
11812 break;
11813
11814 case kIOPMTracePointSystemSleep:
11815 *phaseString = "kIOPMTracePointSystemSleep";
11816 *description = "in EFI/Bootrom after last point of entry to sleep";
11817 break;
11818
11819 case kIOPMTracePointWakePlatformDriver:
11820 *phaseString = "kIOPMTracePointWakePlatformDriver";
11821 *description = "executing platform specific code";
11822 break;
11823
11824
11825 case kIOPMTracePointWakePlatformActions:
11826 *phaseString = "kIOPMTracePointWakePlatformActions";
11827 *description = "calling Wake action callbacks";
11828 break;
11829
11830 case kIOPMTracePointWakeCPUs:
11831 *phaseString = "kIOPMTracePointWakeCPUs";
11832 *description = "starting non-boot CPUs";
11833 break;
11834
11835 case kIOPMTracePointWakeWillPowerOnClients:
11836 *phaseString = "kIOPMTracePointWakeWillPowerOnClients";
11837 *description = "sending kIOMessageSystemWillPowerOn message to kernel and userspace clients";
11838 break;
11839
11840 case kIOPMTracePointWakeWillChangeInterests:
11841 *phaseString = "kIOPMTracePointWakeWillChangeInterests";
11842 *description = "calling rootDomain's clients about upcoming rootDomain's state changes";
11843 break;
11844
11845 case kIOPMTracePointWakeDidChangeInterests:
11846 *phaseString = "kIOPMTracePointWakeDidChangeInterests";
11847 *description = "calling rootDomain's clients about completed rootDomain's state changes";
11848 break;
11849
11850 case kIOPMTracePointWakePowerPlaneDrivers:
11851 *phaseString = "kIOPMTracePointWakePowerPlaneDrivers";
11852 *description = "calling power state change callbacks";
11853 break;
11854
11855 case kIOPMTracePointWakeCapabilityClients:
11856 *phaseString = "kIOPMTracePointWakeCapabilityClients";
11857 *description = "informing clients about current system capabilities";
11858 break;
11859
11860 case kIOPMTracePointWakeApplications:
11861 *phaseString = "kIOPMTracePointWakeApplications";
11862 *description = "sending asynchronous kIOMessageSystemHasPoweredOn message to userspace clients";
11863 break;
11864
11865 case kIOPMTracePointDarkWakeEntry:
11866 *phaseString = "kIOPMTracePointDarkWakeEntry";
11867 *description = "entering darkwake on way to sleep";
11868 break;
11869
11870 case kIOPMTracePointDarkWakeExit:
11871 *phaseString = "kIOPMTracePointDarkWakeExit";
11872 *description = "entering fullwake from darkwake";
11873 break;
11874
11875 default:
11876 *phaseString = NULL;
11877 *description = NULL;
11878 }
11879 }
11880
11881 void
saveFailureData2File()11882 IOPMrootDomain::saveFailureData2File()
11883 {
11884 unsigned int len = 0;
11885 char failureStr[512];
11886 errno_t error;
11887 char *outbuf;
11888 OSNumber *statusCode;
11889 uint64_t pmStatusCode = 0;
11890 uint32_t phaseData = 0;
11891 uint32_t phaseDetail = 0;
11892 bool efiFailure = false;
11893
11894 OSSharedPtr<OSObject> statusCodeProp = copyProperty(kIOPMSleepWakeFailureCodeKey);
11895 statusCode = OSDynamicCast(OSNumber, statusCodeProp.get());
11896 if (statusCode) {
11897 pmStatusCode = statusCode->unsigned64BitValue();
11898 phaseData = pmStatusCode & 0xFFFFFFFF;
11899 phaseDetail = (pmStatusCode >> 32) & 0xFFFFFFFF;
11900 if ((phaseData & 0xFF) == kIOPMTracePointSystemSleep) {
11901 LOG("Sleep Wake failure in EFI\n");
11902 efiFailure = true;
11903 failureStr[0] = 0;
11904 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);
11905 len = (typeof(len))strnlen(failureStr, sizeof(failureStr));
11906 }
11907 }
11908
11909 if (!efiFailure) {
11910 if (PEReadNVRAMProperty(kIOSleepWakeFailurePanic, NULL, &len)) {
11911 swd_flags |= SWD_BOOT_BY_SW_WDOG;
11912 PERemoveNVRAMProperty(kIOSleepWakeFailurePanic);
11913 // dump panic will handle saving nvram data
11914 return;
11915 }
11916
11917 /* Keeping this around for capturing data during power
11918 * button press */
11919
11920 if (!PEReadNVRAMProperty(kIOSleepWakeFailureString, NULL, &len)) {
11921 DLOG("No sleep wake failure string\n");
11922 return;
11923 }
11924 if (len == 0) {
11925 DLOG("Ignoring zero byte SleepWake failure string\n");
11926 goto exit;
11927 }
11928
11929 // if PMStatus code is zero, delete stackshot and return
11930 if (statusCode) {
11931 if (((pmStatusCode & 0xFFFFFFFF) & 0xFF) == 0) {
11932 // there was no sleep wake failure
11933 // this can happen if delete stackshot was called
11934 // before take stackshot completed. Let us delete any
11935 // sleep wake failure data in nvram
11936 DLOG("Deleting stackshot on successful wake\n");
11937 deleteStackshot();
11938 return;
11939 }
11940 }
11941
11942 if (len > sizeof(failureStr)) {
11943 len = sizeof(failureStr);
11944 }
11945 failureStr[0] = 0;
11946 PEReadNVRAMProperty(kIOSleepWakeFailureString, failureStr, &len);
11947 }
11948 if (failureStr[0] != 0) {
11949 error = sleepWakeDebugSaveFile(kSleepWakeFailureStringFile, failureStr, len);
11950 if (error) {
11951 DLOG("Failed to save SleepWake failure string to file. error:%d\n", error);
11952 } else {
11953 DLOG("Saved SleepWake failure string to file.\n");
11954 }
11955 }
11956
11957 if (!OSCompareAndSwap(0, 1, &gRootDomain->swd_lock)) {
11958 goto exit;
11959 }
11960
11961 if (swd_buffer) {
11962 unsigned int len = 0;
11963 errno_t error;
11964 char nvram_var_name_buffer[20];
11965 unsigned int concat_len = 0;
11966 swd_hdr *hdr = NULL;
11967
11968
11969 hdr = (swd_hdr *)swd_buffer;
11970 outbuf = (char *)hdr + hdr->spindump_offset;
11971 OSBoundedArrayRef<char> boundedOutBuf(outbuf, hdr->alloc_size - hdr->spindump_offset);
11972
11973 for (int i = 0; i < 8; i++) {
11974 snprintf(nvram_var_name_buffer, sizeof(nvram_var_name_buffer), "%s%02d", SWD_STACKSHOT_VAR_PREFIX, i + 1);
11975 if (!PEReadNVRAMProperty(nvram_var_name_buffer, NULL, &len)) {
11976 LOG("No SleepWake blob to read beyond chunk %d\n", i);
11977 break;
11978 }
11979 if (PEReadNVRAMProperty(nvram_var_name_buffer, boundedOutBuf.slice(concat_len, len).data(), &len) == FALSE) {
11980 PERemoveNVRAMProperty(nvram_var_name_buffer);
11981 LOG("Could not read the property :-(\n");
11982 break;
11983 }
11984 PERemoveNVRAMProperty(nvram_var_name_buffer);
11985 concat_len += len;
11986 }
11987 LOG("Concatenated length for the SWD blob %d\n", concat_len);
11988
11989 if (concat_len) {
11990 error = sleepWakeDebugSaveFile(kSleepWakeStacksFilename, outbuf, concat_len);
11991 if (error) {
11992 LOG("Failed to save SleepWake zipped data to file. error:%d\n", error);
11993 } else {
11994 LOG("Saved SleepWake zipped data to file.\n");
11995 }
11996 } else {
11997 // There is a sleep wake failure string but no stackshot
11998 // Write a placeholder stacks file so that swd runs
11999 snprintf(outbuf, 20, "%s", "No stackshot data\n");
12000 error = sleepWakeDebugSaveFile(kSleepWakeStacksFilename, outbuf, 20);
12001 if (error) {
12002 LOG("Failed to save SleepWake zipped data to file. error:%d\n", error);
12003 } else {
12004 LOG("Saved SleepWake zipped data to file.\n");
12005 }
12006 }
12007 } else {
12008 LOG("No buffer allocated to save failure stackshot\n");
12009 }
12010
12011
12012 gRootDomain->swd_lock = 0;
12013 exit:
12014 PERemoveNVRAMProperty(kIOSleepWakeFailureString);
12015 return;
12016 }
12017
12018
12019 void
getFailureData(thread_t * thread,char * failureStr,size_t strLen)12020 IOPMrootDomain::getFailureData(thread_t *thread, char *failureStr, size_t strLen)
12021 {
12022 OSSharedPtr<IORegistryIterator> iter;
12023 OSSharedPtr<const OSSymbol> kextName = NULL;
12024 IORegistryEntry * entry;
12025 IOService * node;
12026 bool nodeFound = false;
12027
12028 const void * callMethod = NULL;
12029 const char * objectName = NULL;
12030 uint32_t timeout = getWatchdogTimeout();
12031 const char * phaseString = NULL;
12032 const char * phaseDescription = NULL;
12033
12034 IOPMServiceInterestNotifier *notifier = OSDynamicCast(IOPMServiceInterestNotifier, notifierObject.get());
12035 uint32_t tracePhase = pmTracer->getTracePhase();
12036
12037 *thread = NULL;
12038 if ((tracePhase < kIOPMTracePointSystemSleep) || (tracePhase == kIOPMTracePointDarkWakeEntry)) {
12039 snprintf(failureStr, strLen, "Sleep transition timed out after %d seconds", timeout);
12040 } else {
12041 snprintf(failureStr, strLen, "Wake transition timed out after %d seconds", timeout);
12042 }
12043 tracePhase2String(tracePhase, &phaseString, &phaseDescription);
12044
12045 if (notifierThread) {
12046 if (notifier && (notifier->identifier)) {
12047 objectName = notifier->identifier->getCStringNoCopy();
12048 }
12049 *thread = notifierThread;
12050 } else {
12051 iter = IORegistryIterator::iterateOver(
12052 getPMRootDomain(), gIOPowerPlane, kIORegistryIterateRecursively);
12053
12054 if (iter) {
12055 while ((entry = iter->getNextObject())) {
12056 node = OSDynamicCast(IOService, entry);
12057 if (!node) {
12058 continue;
12059 }
12060 if (OSDynamicCast(IOPowerConnection, node)) {
12061 continue;
12062 }
12063
12064 if (node->getBlockingDriverCall(thread, &callMethod)) {
12065 nodeFound = true;
12066 break;
12067 }
12068 }
12069 }
12070 if (nodeFound) {
12071 kextName = copyKextIdentifierWithAddress((vm_address_t) callMethod);
12072 if (kextName) {
12073 objectName = kextName->getCStringNoCopy();
12074 }
12075 }
12076 }
12077 if (phaseDescription) {
12078 strlcat(failureStr, " while ", strLen);
12079 strlcat(failureStr, phaseDescription, strLen);
12080 strlcat(failureStr, ".", strLen);
12081 }
12082 if (objectName) {
12083 strlcat(failureStr, " Suspected bundle: ", strLen);
12084 strlcat(failureStr, objectName, strLen);
12085 strlcat(failureStr, ".", strLen);
12086 }
12087 if (*thread) {
12088 char threadName[40];
12089 snprintf(threadName, sizeof(threadName), " Thread 0x%llx.", thread_tid(*thread));
12090 strlcat(failureStr, threadName, strLen);
12091 }
12092
12093 DLOG("%s\n", failureStr);
12094 }
12095
12096 struct swd_stackshot_compressed_data {
12097 z_output_func zoutput;
12098 size_t zipped;
12099 uint64_t totalbytes;
12100 uint64_t lastpercent;
12101 IOReturn error;
12102 unsigned outremain;
12103 unsigned outlen;
12104 unsigned writes;
12105 Bytef * outbuf;
12106 };
12107 struct swd_stackshot_compressed_data swd_zip_var = { };
12108
12109 static void *
swd_zs_alloc(void * __unused ref,u_int items,u_int size)12110 swd_zs_alloc(void *__unused ref, u_int items, u_int size)
12111 {
12112 void *result;
12113 LOG("Alloc in zipping %d items of size %d\n", items, size);
12114
12115 result = (void *)(swd_zs_zmem + swd_zs_zoffset);
12116 swd_zs_zoffset += ~31L & (31 + (items * size)); // 32b align for vector crc
12117 LOG("Offset %zu\n", swd_zs_zoffset);
12118 return result;
12119 }
12120
12121 static int
swd_zinput(z_streamp strm,Bytef * buf,unsigned size)12122 swd_zinput(z_streamp strm, Bytef *buf, unsigned size)
12123 {
12124 unsigned len;
12125
12126 len = strm->avail_in;
12127
12128 if (len > size) {
12129 len = size;
12130 }
12131 if (len == 0) {
12132 return 0;
12133 }
12134
12135 if (strm->next_in != (Bytef *) strm) {
12136 memcpy(buf, strm->next_in, len);
12137 } else {
12138 bzero(buf, len);
12139 }
12140
12141 strm->adler = z_crc32(strm->adler, buf, len);
12142
12143 strm->avail_in -= len;
12144 strm->next_in += len;
12145 strm->total_in += len;
12146
12147 return (int)len;
12148 }
12149
12150 static int
swd_zoutput(z_streamp strm,Bytef * buf,unsigned len)12151 swd_zoutput(z_streamp strm, Bytef *buf, unsigned len)
12152 {
12153 unsigned int i = 0;
12154 // if outlen > max size don't add to the buffer
12155 assert(buf != NULL);
12156 if (strm && buf) {
12157 if (swd_zip_var.outlen + len > SWD_COMPRESSED_BUFSIZE) {
12158 LOG("No space to GZIP... not writing to NVRAM\n");
12159 return len;
12160 }
12161 }
12162 for (i = 0; i < len; i++) {
12163 *(swd_zip_var.outbuf + swd_zip_var.outlen + i) = *(buf + i);
12164 }
12165 swd_zip_var.outlen += len;
12166 return len;
12167 }
12168
12169 static void
swd_zs_free(void * __unused ref,void * __unused ptr)12170 swd_zs_free(void * __unused ref, void * __unused ptr)
12171 {
12172 }
12173
12174 static int
swd_compress(char * inPtr,char * outPtr,size_t numBytes)12175 swd_compress(char *inPtr, char *outPtr, size_t numBytes)
12176 {
12177 int wbits = 12;
12178 int memlevel = 3;
12179
12180 if (((unsigned int) numBytes) != numBytes) {
12181 return 0;
12182 }
12183
12184 if (!swd_zs.zalloc) {
12185 swd_zs.zalloc = swd_zs_alloc;
12186 swd_zs.zfree = swd_zs_free;
12187 if (deflateInit2(&swd_zs, Z_BEST_SPEED, Z_DEFLATED, wbits + 16, memlevel, Z_DEFAULT_STRATEGY)) {
12188 // allocation failed
12189 bzero(&swd_zs, sizeof(swd_zs));
12190 // swd_zs_zoffset = 0;
12191 } else {
12192 LOG("PMRD inited the zlib allocation routines\n");
12193 }
12194 }
12195
12196 swd_zip_var.zipped = 0;
12197 swd_zip_var.totalbytes = 0; // should this be the max that we have?
12198 swd_zip_var.lastpercent = 0;
12199 swd_zip_var.error = kIOReturnSuccess;
12200 swd_zip_var.outremain = 0;
12201 swd_zip_var.outlen = 0;
12202 swd_zip_var.writes = 0;
12203 swd_zip_var.outbuf = (Bytef *)outPtr;
12204
12205 swd_zip_var.totalbytes = numBytes;
12206
12207 swd_zs.avail_in = 0;
12208 swd_zs.next_in = NULL;
12209 swd_zs.avail_out = 0;
12210 swd_zs.next_out = NULL;
12211
12212 deflateResetWithIO(&swd_zs, swd_zinput, swd_zoutput);
12213
12214 z_stream *zs;
12215 int zr;
12216 zs = &swd_zs;
12217
12218 while (swd_zip_var.error >= 0) {
12219 if (!zs->avail_in) {
12220 zs->next_in = (unsigned char *)inPtr ? (Bytef *)inPtr : (Bytef *)zs; /* zero marker? */
12221 zs->avail_in = (unsigned int) numBytes;
12222 }
12223 if (!zs->avail_out) {
12224 zs->next_out = (Bytef *)zs;
12225 zs->avail_out = UINT32_MAX;
12226 }
12227 zr = deflate(zs, Z_NO_FLUSH);
12228 if (Z_STREAM_END == zr) {
12229 break;
12230 }
12231 if (zr != Z_OK) {
12232 LOG("ZERR %d\n", zr);
12233 swd_zip_var.error = zr;
12234 } else {
12235 if (zs->total_in == numBytes) {
12236 break;
12237 }
12238 }
12239 }
12240
12241 //now flush the stream
12242 while (swd_zip_var.error >= 0) {
12243 if (!zs->avail_out) {
12244 zs->next_out = (Bytef *)zs;
12245 zs->avail_out = UINT32_MAX;
12246 }
12247 zr = deflate(zs, Z_FINISH);
12248 if (Z_STREAM_END == zr) {
12249 break;
12250 }
12251 if (zr != Z_OK) {
12252 LOG("ZERR %d\n", zr);
12253 swd_zip_var.error = zr;
12254 } else {
12255 if (zs->total_in == numBytes) {
12256 LOG("Total output size %d\n", swd_zip_var.outlen);
12257 break;
12258 }
12259 }
12260 }
12261
12262 return swd_zip_var.outlen;
12263 }
12264
12265 void
deleteStackshot()12266 IOPMrootDomain::deleteStackshot()
12267 {
12268 if (!OSCompareAndSwap(0, 1, &gRootDomain->swd_lock)) {
12269 // takeStackshot hasn't completed
12270 return;
12271 }
12272 LOG("Deleting any sleepwake failure data in nvram\n");
12273
12274 PERemoveNVRAMProperty(kIOSleepWakeFailureString);
12275 char nvram_var_name_buf[20];
12276 for (int i = 0; i < 8; i++) {
12277 snprintf(nvram_var_name_buf, sizeof(nvram_var_name_buf), "%s%02d", SWD_STACKSHOT_VAR_PREFIX, i + 1);
12278 if (PERemoveNVRAMProperty(nvram_var_name_buf) == false) {
12279 LOG("Removing %s returned false\n", nvram_var_name_buf);
12280 }
12281 }
12282 // force NVRAM sync
12283 if (PEWriteNVRAMProperty(kIONVRAMSyncNowPropertyKey, kIONVRAMSyncNowPropertyKey, (unsigned int) strlen(kIONVRAMSyncNowPropertyKey)) == false) {
12284 DLOG("Failed to force nvram sync\n");
12285 }
12286 gRootDomain->swd_lock = 0;
12287 }
12288
12289 void
takeStackshot(bool wdogTrigger)12290 IOPMrootDomain::takeStackshot(bool wdogTrigger)
12291 {
12292 swd_hdr * hdr = NULL;
12293 int cnt = 0;
12294 int max_cnt;
12295 pid_t pid = 0;
12296 kern_return_t kr = KERN_SUCCESS;
12297 uint64_t flags;
12298
12299 char * dstAddr;
12300 uint32_t size;
12301 uint32_t bytesRemaining;
12302 unsigned bytesWritten = 0;
12303
12304 char failureStr[512];
12305 thread_t thread = NULL;
12306 const char * swfPanic = "swfPanic";
12307
12308 uint32_t bufSize;
12309 int success = 0;
12310
12311 #if defined(__i386__) || defined(__x86_64__)
12312 const bool concise = false;
12313 #else
12314 const bool concise = true;
12315 #endif
12316
12317 if (!OSCompareAndSwap(0, 1, &gRootDomain->swd_lock)) {
12318 return;
12319 }
12320
12321 failureStr[0] = 0;
12322 if ((kIOSleepWakeWdogOff & gIOKitDebug) || systemBooting || systemShutdown || gWillShutdown) {
12323 return;
12324 }
12325
12326 if (wdogTrigger) {
12327 getFailureData(&thread, failureStr, sizeof(failureStr));
12328
12329 if (concise || (PEGetCoprocessorVersion() >= kCoprocessorVersion2)) {
12330 goto skip_stackshot;
12331 }
12332 } else {
12333 AbsoluteTime now;
12334 uint64_t nsec;
12335 clock_get_uptime(&now);
12336 SUB_ABSOLUTETIME(&now, &gIOLastWakeAbsTime);
12337 absolutetime_to_nanoseconds(now, &nsec);
12338 snprintf(failureStr, sizeof(failureStr), "Power button pressed during wake transition after %u ms.\n", ((int)((nsec) / NSEC_PER_MSEC)));
12339 }
12340
12341 if (swd_buffer == NULL) {
12342 sleepWakeDebugMemAlloc();
12343 if (swd_buffer == NULL) {
12344 return;
12345 }
12346 }
12347 hdr = (swd_hdr *)swd_buffer;
12348 bufSize = hdr->alloc_size;
12349
12350 dstAddr = (char*)hdr + hdr->spindump_offset;
12351 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;
12352
12353 /* If not wdogTrigger only take kernel tasks stackshot
12354 */
12355 if (wdogTrigger) {
12356 pid = -1;
12357 max_cnt = 3;
12358 } else {
12359 pid = 0;
12360 max_cnt = 2;
12361 }
12362
12363 /* Attempt to take stackshot with all ACTIVE_KERNEL_THREADS
12364 * If we run out of space, take stackshot with only kernel task
12365 */
12366 while (success == 0 && cnt < max_cnt) {
12367 bytesRemaining = bufSize - hdr->spindump_offset;
12368 cnt++;
12369 DLOG("Taking snapshot. bytesRemaining: %d\n", bytesRemaining);
12370
12371 size = bytesRemaining;
12372 kr = stack_snapshot_from_kernel(pid, dstAddr, size, flags, 0, 0, &bytesWritten);
12373 DLOG("stack_snapshot_from_kernel returned 0x%x. pid: %d bufsize:0x%x flags:0x%llx bytesWritten: %d\n",
12374 kr, pid, size, flags, bytesWritten);
12375 if (kr == KERN_INSUFFICIENT_BUFFER_SIZE) {
12376 if (pid == -1) {
12377 pid = 0;
12378 } else if (flags & STACKSHOT_INCLUDE_DRIVER_THREADS_IN_KERNEL) {
12379 flags = flags & ~STACKSHOT_INCLUDE_DRIVER_THREADS_IN_KERNEL;
12380 } else {
12381 LOG("Insufficient buffer size for only kernel task\n");
12382 break;
12383 }
12384 }
12385 if (kr == KERN_SUCCESS) {
12386 if (bytesWritten == 0) {
12387 MSG("Failed to get stackshot(0x%x) bufsize:0x%x flags:0x%llx\n", kr, size, flags);
12388 continue;
12389 }
12390 bytesRemaining -= bytesWritten;
12391 hdr->spindump_size = (bufSize - bytesRemaining - hdr->spindump_offset);
12392
12393 memset(hdr->reason, 0x20, sizeof(hdr->reason));
12394
12395 // Compress stackshot and save to NVRAM
12396 {
12397 char *outbuf = (char *)swd_compressed_buffer;
12398 int outlen = 0;
12399 int num_chunks = 0;
12400 int max_chunks = 0;
12401 int leftover = 0;
12402 char nvram_var_name_buffer[20];
12403
12404 outlen = swd_compress((char*)hdr + hdr->spindump_offset, outbuf, bytesWritten);
12405
12406 if (outlen) {
12407 max_chunks = outlen / (2096 - 200);
12408 leftover = outlen % (2096 - 200);
12409
12410 if (max_chunks < 8) {
12411 for (num_chunks = 0; num_chunks < max_chunks; num_chunks++) {
12412 snprintf(nvram_var_name_buffer, sizeof(nvram_var_name_buffer), "%s%02d", SWD_STACKSHOT_VAR_PREFIX, num_chunks + 1);
12413 if (PEWriteNVRAMPropertyWithCopy(nvram_var_name_buffer, (outbuf + (num_chunks * (2096 - 200))), (2096 - 200)) == FALSE) {
12414 LOG("Failed to update NVRAM %d\n", num_chunks);
12415 break;
12416 }
12417 }
12418 if (leftover) {
12419 snprintf(nvram_var_name_buffer, sizeof(nvram_var_name_buffer), "%s%02d", SWD_STACKSHOT_VAR_PREFIX, num_chunks + 1);
12420 if (PEWriteNVRAMPropertyWithCopy(nvram_var_name_buffer, (outbuf + (num_chunks * (2096 - 200))), leftover) == FALSE) {
12421 LOG("Failed to update NVRAM with leftovers\n");
12422 }
12423 }
12424 success = 1;
12425 LOG("Successfully saved stackshot to NVRAM\n");
12426 } else {
12427 if (pid == -1) {
12428 LOG("Compressed failure stackshot is too large. size=%d bytes\n", outlen);
12429 pid = 0;
12430 } else if (flags & STACKSHOT_INCLUDE_DRIVER_THREADS_IN_KERNEL) {
12431 LOG("Compressed failure stackshot of kernel+dexts is too large size=%d bytes\n", outlen);
12432 flags = flags & ~STACKSHOT_INCLUDE_DRIVER_THREADS_IN_KERNEL;
12433 } else {
12434 LOG("Compressed failure stackshot of only kernel is too large size=%d bytes\n", outlen);
12435 break;
12436 }
12437 }
12438 }
12439 }
12440 }
12441 }
12442
12443 if (failureStr[0]) {
12444 // append sleep-wake failure code
12445 char traceCode[80];
12446 snprintf(traceCode, sizeof(traceCode), "\nFailure code:: 0x%08x %08x\n",
12447 pmTracer->getTraceData(), pmTracer->getTracePhase());
12448 strlcat(failureStr, traceCode, sizeof(failureStr));
12449 if (PEWriteNVRAMProperty(kIOSleepWakeFailureString, failureStr, (unsigned int) strnlen(failureStr, sizeof(failureStr))) == false) {
12450 DLOG("Failed to write SleepWake failure string\n");
12451 }
12452 }
12453
12454 // force NVRAM sync
12455 if (PEWriteNVRAMProperty(kIONVRAMSyncNowPropertyKey, kIONVRAMSyncNowPropertyKey, (unsigned int) strlen(kIONVRAMSyncNowPropertyKey)) == false) {
12456 DLOG("Failed to force nvram sync\n");
12457 }
12458
12459 skip_stackshot:
12460 if (wdogTrigger) {
12461 if (PEGetCoprocessorVersion() < kCoprocessorVersion2) {
12462 if (swd_flags & SWD_BOOT_BY_SW_WDOG) {
12463 // If current boot is due to this watch dog trigger restart in previous boot,
12464 // then don't trigger again until at least 1 successful sleep & wake.
12465 if (!(sleepCnt && (displayWakeCnt || darkWakeCnt))) {
12466 LOG("Shutting down due to repeated Sleep/Wake failures\n");
12467 updateTasksSuspend(kTasksSuspendSuspended, kTasksSuspendNoChange);
12468 PEHaltRestart(kPEHaltCPU);
12469 return;
12470 }
12471 }
12472 if (gSwdPanic == 0) {
12473 LOG("Calling panic prevented by swd_panic boot-args. Calling restart");
12474 updateTasksSuspend(kTasksSuspendSuspended, kTasksSuspendNoChange);
12475 PEHaltRestart(kPERestartCPU);
12476 }
12477 }
12478 if (!concise && (PEWriteNVRAMProperty(kIOSleepWakeFailurePanic, swfPanic, (unsigned int) strlen(swfPanic)) == false)) {
12479 DLOG("Failed to write SleepWake failure panic key\n");
12480 }
12481 #if defined(__x86_64__)
12482 if (thread) {
12483 panic_with_thread_context(0, NULL, DEBUGGER_OPTION_ATTEMPTCOREDUMPANDREBOOT, thread, "%s", failureStr);
12484 } else
12485 #endif /* defined(__x86_64__) */
12486 {
12487 panic_with_options(0, NULL, DEBUGGER_OPTION_ATTEMPTCOREDUMPANDREBOOT, "%s", failureStr);
12488 }
12489 } else {
12490 gRootDomain->swd_lock = 0;
12491 return;
12492 }
12493 }
12494
12495 void
sleepWakeDebugMemAlloc()12496 IOPMrootDomain::sleepWakeDebugMemAlloc()
12497 {
12498 vm_size_t size = SWD_STACKSHOT_SIZE + SWD_COMPRESSED_BUFSIZE + SWD_ZLIB_BUFSIZE;
12499
12500 swd_hdr *hdr = NULL;
12501 void *bufPtr = NULL;
12502
12503 OSSharedPtr<IOBufferMemoryDescriptor> memDesc;
12504
12505
12506 if (kIOSleepWakeWdogOff & gIOKitDebug) {
12507 return;
12508 }
12509
12510 if (!OSCompareAndSwap(0, 1, &gRootDomain->swd_lock)) {
12511 return;
12512 }
12513
12514 memDesc = IOBufferMemoryDescriptor::inTaskWithOptions(
12515 kernel_task, kIODirectionIn | kIOMemoryMapperNone,
12516 size);
12517 if (memDesc == NULL) {
12518 DLOG("Failed to allocate Memory descriptor for sleepWake debug\n");
12519 goto exit;
12520 }
12521
12522 bufPtr = memDesc->getBytesNoCopy();
12523
12524 // Carve out memory for zlib routines
12525 swd_zs_zmem = (vm_offset_t)bufPtr;
12526 bufPtr = (char *)bufPtr + SWD_ZLIB_BUFSIZE;
12527
12528 // Carve out memory for compressed stackshots
12529 swd_compressed_buffer = bufPtr;
12530 bufPtr = (char *)bufPtr + SWD_COMPRESSED_BUFSIZE;
12531
12532 // Remaining is used for holding stackshot
12533 hdr = (swd_hdr *)bufPtr;
12534 memset(hdr, 0, sizeof(swd_hdr));
12535
12536 hdr->signature = SWD_HDR_SIGNATURE;
12537 hdr->alloc_size = SWD_STACKSHOT_SIZE;
12538
12539 hdr->spindump_offset = sizeof(swd_hdr);
12540 swd_buffer = (void *)hdr;
12541 swd_memDesc = os::move(memDesc);
12542 DLOG("SleepWake debug buffer size:0x%x spindump offset:0x%x\n", hdr->alloc_size, hdr->spindump_offset);
12543
12544 exit:
12545 gRootDomain->swd_lock = 0;
12546 }
12547
12548 void
sleepWakeDebugSpinDumpMemAlloc()12549 IOPMrootDomain::sleepWakeDebugSpinDumpMemAlloc()
12550 {
12551 #if UNUSED
12552 vm_size_t size = SWD_SPINDUMP_SIZE;
12553
12554 swd_hdr *hdr = NULL;
12555
12556 OSSharedPtr<IOBufferMemoryDescriptor> memDesc;
12557
12558 if (!OSCompareAndSwap(0, 1, &gRootDomain->swd_lock)) {
12559 return;
12560 }
12561
12562 memDesc = IOBufferMemoryDescriptor::inTaskWithOptions(
12563 kernel_task, kIODirectionIn | kIOMemoryMapperNone,
12564 SWD_SPINDUMP_SIZE);
12565
12566 if (memDesc == NULL) {
12567 DLOG("Failed to allocate Memory descriptor for sleepWake debug spindump\n");
12568 goto exit;
12569 }
12570
12571
12572 hdr = (swd_hdr *)memDesc->getBytesNoCopy();
12573 memset(hdr, 0, sizeof(swd_hdr));
12574
12575 hdr->signature = SWD_HDR_SIGNATURE;
12576 hdr->alloc_size = size;
12577
12578 hdr->spindump_offset = sizeof(swd_hdr);
12579 swd_spindump_buffer = (void *)hdr;
12580 swd_spindump_memDesc = os::move(memDesc);
12581
12582 exit:
12583 gRootDomain->swd_lock = 0;
12584 #endif /* UNUSED */
12585 }
12586
12587 void
sleepWakeDebugEnableWdog()12588 IOPMrootDomain::sleepWakeDebugEnableWdog()
12589 {
12590 }
12591
12592 bool
sleepWakeDebugIsWdogEnabled()12593 IOPMrootDomain::sleepWakeDebugIsWdogEnabled()
12594 {
12595 return !systemBooting && !systemShutdown && !gWillShutdown;
12596 }
12597
12598 void
sleepWakeDebugSaveSpinDumpFile()12599 IOPMrootDomain::sleepWakeDebugSaveSpinDumpFile()
12600 {
12601 swd_hdr *hdr = NULL;
12602 errno_t error = EIO;
12603
12604 if (swd_spindump_buffer && gSpinDumpBufferFull) {
12605 hdr = (swd_hdr *)swd_spindump_buffer;
12606
12607 error = sleepWakeDebugSaveFile("/var/tmp/SleepWakeDelayStacks.dump",
12608 (char*)hdr + hdr->spindump_offset, hdr->spindump_size);
12609
12610 if (error) {
12611 return;
12612 }
12613
12614 sleepWakeDebugSaveFile("/var/tmp/SleepWakeDelayLog.dump",
12615 (char*)hdr + offsetof(swd_hdr, UUID),
12616 sizeof(swd_hdr) - offsetof(swd_hdr, UUID));
12617
12618 gSpinDumpBufferFull = false;
12619 }
12620 }
12621
12622 errno_t
sleepWakeDebugSaveFile(const char * name,char * buf,int len)12623 IOPMrootDomain::sleepWakeDebugSaveFile(const char *name, char *buf, int len)
12624 {
12625 struct vnode *vp = NULL;
12626 vfs_context_t ctx = vfs_context_create(vfs_context_current());
12627 kauth_cred_t cred = vfs_context_ucred(ctx);
12628 struct vnode_attr va;
12629 errno_t error = EIO;
12630
12631 if (vnode_open(name, (O_CREAT | FWRITE | O_NOFOLLOW),
12632 S_IRUSR | S_IRGRP | S_IROTH, VNODE_LOOKUP_NOFOLLOW, &vp, ctx) != 0) {
12633 LOG("Failed to open the file %s\n", name);
12634 swd_flags |= SWD_FILEOP_ERROR;
12635 goto exit;
12636 }
12637 VATTR_INIT(&va);
12638 VATTR_WANTED(&va, va_nlink);
12639 /* Don't dump to non-regular files or files with links. */
12640 if (vp->v_type != VREG ||
12641 vnode_getattr(vp, &va, ctx) || va.va_nlink != 1) {
12642 LOG("Bailing as this is not a regular file\n");
12643 swd_flags |= SWD_FILEOP_ERROR;
12644 goto exit;
12645 }
12646 VATTR_INIT(&va);
12647 VATTR_SET(&va, va_data_size, 0);
12648 vnode_setattr(vp, &va, ctx);
12649
12650
12651 if (buf != NULL) {
12652 error = vn_rdwr(UIO_WRITE, vp, buf, len, 0,
12653 UIO_SYSSPACE, IO_NODELOCKED | IO_UNIT, cred, (int *) NULL, vfs_context_proc(ctx));
12654 if (error != 0) {
12655 LOG("Failed to save sleep wake log. err 0x%x\n", error);
12656 swd_flags |= SWD_FILEOP_ERROR;
12657 } else {
12658 DLOG("Saved %d bytes to file %s\n", len, name);
12659 }
12660 }
12661
12662 exit:
12663 if (vp) {
12664 vnode_close(vp, FWRITE, ctx);
12665 }
12666 if (ctx) {
12667 vfs_context_rele(ctx);
12668 }
12669
12670 return error;
12671 }
12672
12673 #else /* defined(__i386__) || defined(__x86_64__) */
12674
12675 void
sleepWakeDebugTrig(bool restart)12676 IOPMrootDomain::sleepWakeDebugTrig(bool restart)
12677 {
12678 if (restart) {
12679 if (gSwdPanic == 0) {
12680 return;
12681 }
12682 panic("Sleep/Wake hang detected");
12683 return;
12684 }
12685 }
12686
12687 void
takeStackshot(bool restart)12688 IOPMrootDomain::takeStackshot(bool restart)
12689 {
12690 #pragma unused(restart)
12691 }
12692
12693 void
deleteStackshot()12694 IOPMrootDomain::deleteStackshot()
12695 {
12696 }
12697
12698 void
sleepWakeDebugMemAlloc()12699 IOPMrootDomain::sleepWakeDebugMemAlloc()
12700 {
12701 }
12702
12703 void
saveFailureData2File()12704 IOPMrootDomain::saveFailureData2File()
12705 {
12706 }
12707
12708 void
sleepWakeDebugEnableWdog()12709 IOPMrootDomain::sleepWakeDebugEnableWdog()
12710 {
12711 }
12712
12713 bool
sleepWakeDebugIsWdogEnabled()12714 IOPMrootDomain::sleepWakeDebugIsWdogEnabled()
12715 {
12716 return false;
12717 }
12718
12719 void
sleepWakeDebugSaveSpinDumpFile()12720 IOPMrootDomain::sleepWakeDebugSaveSpinDumpFile()
12721 {
12722 }
12723
12724 errno_t
sleepWakeDebugSaveFile(const char * name,char * buf,int len)12725 IOPMrootDomain::sleepWakeDebugSaveFile(const char *name, char *buf, int len)
12726 {
12727 return 0;
12728 }
12729
12730 #endif /* defined(__i386__) || defined(__x86_64__) */
12731
12732